在Python/psycopg2中迭代结果集时更新项
我在数据库中有一个实体列表,我检索并迭代这些实体,如下所示:在Python/psycopg2中迭代结果集时更新项,python,postgresql,python-3.x,psycopg2,Python,Postgresql,Python 3.x,Psycopg2,我在数据库中有一个实体列表,我检索并迭代这些实体,如下所示: import psycopg2 cur.execute("select * from items") rows = cur.fetchall() for x in rows: # some stuff 我希望能够更新循环中实体上的一些字段 for x in rows: # some stuff if some_condition: curr.execute("update i
import psycopg2
cur.execute("select * from items")
rows = cur.fetchall()
for x in rows:
# some stuff
我希望能够更新循环中实体上的一些字段
for x in rows:
# some stuff
if some_condition:
curr.execute("update items set....
是否可以在循环内部执行此操作?如果没有,解决办法是什么?循环后立即更新所有事务对我不起作用。我认为光标将保留事务,因此您必须在每个循环中处理这些事务。在sql中完成所有操作。@ClodoaldoNeto,mysql?@Kuqa不,仅使用sql而不是Python。