I have a function which updates a single row in 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))
Is it ok to use this function multiple (couple thousand) times and do conn.commit()
afterwards, like this:
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()
Or should I pass curr
instead of conn
to update_one_row
?
I am aware of curr.executemany()
, but i prefer explicit loop. Is there a performance difference?
Overall I am quite lost on the usage of cursors and when to commit.