Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 910
wstomv Avatar asked Dec 10 '17 17:12

wstomv


People also ask

What is DF To_sql?

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)


1 Answers

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

like image 149
MaxU - stop WAR against UA Avatar answered Oct 19 '22 16:10

MaxU - stop WAR against UA