pandas to mysql 错误:“ValueError: 数据库风格 mysql 不受支持”



我需要将数据帧中的数据加载到 mysql 中。我尝试使用df.to_sql,遵循此文档,建议使用:.to_sql(name, con, flavor='mysql', if_exists='fail', index=True, index_label=None)

我收到此错误:ValueError: database flavors mysql is not supported

如何绕过此问题?

风格 'mysql' 在 pandas 版本 0.19 中已弃用。您必须使用 sqlalchemy 中的引擎来创建与数据库的连接。

from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://USER:PASSWORD@HOST/DATABASE")
df.to_sql(name, con=engine, if_exists='fail', index=True, index_label=None)

定义引擎时,需要指定用户、密码、主机和数据库。

最新更新