【发布时间】:2017-04-24 14:52:31
【问题描述】:
在 psycopg2 中,如果我有一张桌子:
+------+-----+-------+
| name | age | color |
+------+-----+-------+
| Bob | 25 | red |
| Bill | 50 | blue |
| Jane | 45 | black |
+------+-----+-------+
如果我这样做cursor.execute("SELECT * FROM mySchema.this_table LIMIT 1")
然后我检查颜色是否存在:
colnames = [desc[0] for desc in cursor.description]
然后在 colnames 中搜索“color”
然后我想我得到了这一行:
myrow = importCursor.fetchone()
但是如何获得该行的“颜色”值?
我试过color = importCursor.fetchone()['color']
但这不起作用。
如何获取该 SELECT 语句返回的行的颜色值?我不知道表中有多少列,或者“颜色”列是否始终是第 3 列我必须为此表中的一堆列执行此操作(检查是否存在,如果存在,则返回列行的值)所以一个有效的方法是最好的!
【问题讨论】:
-
cursor.execute('select color from myschema.this_table limit 1'),color = cursor.fetchall()
标签: python sql postgresql cursor psycopg2