Executing an insert query on each row in results: psycopg2.ProgrammingError: no results to fetch
Executing an insert query on each row in results: psycopg2.ProgrammingError: no results to fetch
我在这里错过了什么愚蠢的东西:
>>> 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,)
看起来 psycopg2 不允许交错查询(尽管 PostgreSQL 可以在后端做到这一点)。如果初始查询不是很大,最简单的解决方案是将结果合并到一个列表中——只需将 from row in cur:
更改为 from row in cur.fetchall():
,你应该是对的。
我在这里错过了什么愚蠢的东西:
>>> 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,)
看起来 psycopg2 不允许交错查询(尽管 PostgreSQL 可以在后端做到这一点)。如果初始查询不是很大,最简单的解决方案是将结果合并到一个列表中——只需将 from row in cur:
更改为 from row in cur.fetchall():
,你应该是对的。