我有一个函数可以更新 db 中的单行。
def update_one_row(conn, condition, value):
with conn.cursor() as curr:
curr.execute("""UPDATE persons p
SET p.age=%s
WHERE p.name=%s;""",
(value, condition))
是否可以多次(几千次)使用此功能并在conn.commit()
之后执行,如下所示:
from pymysql import Connect
connect_args = {...}
conn = Connect(**connect_args)
for condition, value in iterable_of_conditions_values:
update_one_row(conn, condition, value)
# Here I visually inspect in jupyter notebook if things went as expected and I accidentaly did not screw up
conn.commit()
或者我应该通过而curr
不是conn
update_one_row
?
我知道curr.executemany()
,但我更喜欢显式循环。有性能差异吗?
总的来说,我对游标的使用和何时提交非常迷茫。