我正在使用python3,postgress 10和Psycopg2来查询多条记录
import psycopg2
conn = psycopg2.connect(<my connection string>)
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
for id, field1 from cur.fetchall():
print(id,field1)
#todo: how up update field1 to be f(field1) where f is an arbitrary python function
我的问题是:如何更新我正在读取的行的值,并将field1的值设置为某些基于python的任意计算
编辑:目的是更新表中的行
1 回答
你需要另一个光标,例如:
但请注意,这涉及与所选行一样多的更新,这显然效率不高 . 可以使用psycopg2.extras.execute_values():在单个查询中完成更新