pandas 将表写入MySQL:“无法回滚" [英] Pandas Write table to MySQL: "unable to rollback"

查看:312
本文介绍了 pandas 将表写入MySQL:“无法回滚"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助才能正常工作.我有一个pd.DataFrame (df),我需要将其加载到MySQL数据库中.我不明白该错误消息的含义以及解决方法.

I need help to get this working. I have a pd.DataFrame (df), which I need to load to a MySQL database. I don't understand what the error message means and how to fix it.

任何帮助将不胜感激.

这是我尝试过的:

    import MySQLdb
    from pandas.io import sql

    #METHOD 1 
    db=MySQLdb.connect(host="***",port=***,user="***",passwd="***",db="***")
    df.to_sql(con=db, name='forecast', if_exists='replace', flavor='mysql')
    ##Also tried
    sql.write_frame(df, con=db, name='forecast', if_exists='replace', flavor='mysql')

   **DatabaseError**: Execution failed on sql: SHOW TABLES LIKE %s
   (2006, 'MySQL server has gone away')
   unable to rollback


   #METHOD 2: using sqlalchemy
   from sqlalchemy import create_engine

   engine =   create_engine("mysql+mysqldb://**username***:**passwd**@***host***:3306/**dbname**")
   conn = engine.raw_connection()
   df.to_sql(name='demand_forecast_t', con=conn,if_exists='replace',    flavor='mysql',index=False, index_label='rowID')
   conn.close()

错误消息是:

**OperationalError**: DatabaseError: Execution failed on sql: SHOW TABLES LIKE %s
(2006, 'MySQL server has gone away') unable to rollback

推荐答案

在使用sqlalchemy时,您应该传递引擎而不是原始连接:

When using sqlalchemy, you should pass the engine and not the raw connection:

engine = create_engine("mysql+mysqldb://...")
df.to_sql('demand_forecast_t', engine, if_exists='replace', index=False)

不建议使用不带sqlalchemy的MySQL编写(因此指定flavor='mysql').

Writing to MySQL without sqlalchemy (so with specifying flavor='mysql') is deprecated.

如果问题在于您一次编写的框架太大,则可以使用chunksize关键字(请参见

When the problem is that you have a too large frame to write at once, you can use the chunksize keyword (see the docstring). Eg:

df.to_sql('demand_forecast_t', engine, if_exists='replace', chunksize=10000)

这篇关于 pandas 将表写入MySQL:“无法回滚"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆