使用python、psycopg2和postgresq在python中直接引用列名

2024-09-30 10:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python、psycopg2和postgresql。想办法吗 按名称直接引用列。参见下面的示例。你知道吗

TABLE testtable
testcolumn1 | testcolumn2 | testcolumn3 
------------+-------------+------------
 horse      | cow         | goat
 simple     | code        | test
 cant       | figure      | this


conn = psycopg2.connect("dbname='test' user='me' password='pass'")
cursor = conn.cursor()
cursor.execute("select * from testtable")
cursor_rows = cursor.fetchall()
for row in cursor_rows:    
    print(row['testcolumn2'])

# the result of the loop should be 
cow
code
figure

有没有这样的方法可以直接引用列名?你知道吗

谢谢你的帮助


Tags: thetest名称postgresqlcodeconncursorpsycopg2

热门问题