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

和自甴很熟 提交于 2019-12-07 08:42:55

问题


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



来源:https://stackoverflow.com/questions/47741775/does-the-pandas-dataframe-to-sql-function-require-a-subsequent-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!