Does the Pandas DataFrame.to_sql() function requir

2020-07-14 11:58发布

问题:

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

回答1:

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