sqldatabasepandassqlalchemycommit

Does the Pandas DataFrame.to_sql() function require a subsequent commit()?


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())?


Solution

  • 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)
    

    And according to the SQL Alchemy docs executemany issues commit at the end.

    For SQLite connection commit is called explicitly:

    def run_transaction(self):
        cur = self.con.cursor()
        try:
            yield cur
            self.con.commit()
        except:
            self.con.rollback()
            raise
        finally:
            cur.close()