对结果中的每一行执行插入查询:psycopg2.ProgrammingError:无结果获取

时间:2015-05-14 17:57:17

标签: psycopg2 psql

我在这里失去了什么愚蠢的事情:

>>> cur.execute("select id from tracks")
>>> for row in cur:
...     story = random.choice(fortunes) + random.choice(fortunes)
...     cur.execute("update tracks set story=%s where id=%s", (story, row[0]))
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: no results to fetch

似乎是结果:

>>> cur.execute("select id from tracks")
>>> for row in cur:
...     print(row)
... 
(8,)
(45,)
(12,)
(64,)
(1,)
(6,)

1 个答案:

答案 0 :(得分:1)

看起来psycopg2不允许交错查询(尽管PostgreSQL可以在后端执行)。如果初始查询不是很大,那么最简单的解决方案就是将结果合并到一个列表中 - 只需将from row in cur:更改为from row in cur.fetchall():,您应该是对的。

相关问题