问题
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