The documentation for the Pandas function to_sql()
available for DataFrame
objects (see to_sql()
documentation) does not state that a commit()
call on the connection is needed (or recommended) to persist the update.
Can I safely assume that DataFrame.to_sql('table_name', con)
will always automatically commit the changes (like in: con.commit()
)?
DataFrame - to_sql() function. The to_sql() function is used to write records stored in a DataFrame to a SQL database. Syntax: DataFrame.to_sql(self, name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None)
Yes, at the end of the day it will be commited automatically.
Pandas calls SQLAlchemy method executemany
(for SQL Alchemy connections):
conn.executemany(self.insert_statement(), data_list)
for SQLite connection:
def run_transaction(self):
cur = self.con.cursor()
try:
yield cur
self.con.commit()
except:
self.con.rollback()
raise
finally:
cur.close()
And due to the SQL Alchemy docs executemany
issues commit
at the end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With