Meta commands in Psycopg2 - \d not working

自作多情 提交于 2020-01-23 05:37:05

问题


I wish to list all the column names of a table using psycopg2 package of Python (2.7). But I am unable to execute the following query -

cur.execute("\d my_table");
psycopg2.ProgrammingError: syntax error at or near "\"

Is there an alternate as to how I can list the column names of a table using psycopg2 ? Please point out any duplicates. Thanks !


回答1:


Command line psql has some shortcuts like \d but it's not part of SQL. What you need is to query information_schema:

SELECT column_name FROM information_schema.columns WHERE table_name = 'my_table';

EDIT: It's really an important information that the command line psql -E will echo SQL queries used to implement \d and other backslash commands (whenever you use one of them in the psql prompt) as @piro has written in comment. This way you get what you want very easily.
Thanks @piro!



来源:https://stackoverflow.com/questions/31532489/meta-commands-in-psycopg2-d-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!