gpt4 book ai didi

python - 不支持 pandas dataframe 到 mysql db 错误数据库风格 mysql

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:05 25 4
gpt4 key购买 nike

我在 pandas dataframe df 中有一张表。

   product_id_x    product_id_y    count       date
0 288472 288473 1 2016-11-08 04:02:07
1 288473 2933696 1 2016-11-08 04:02:07
2 288473 85694162 1 2016-11-08 04:02:07

我想把这张表保存到mysql数据库中。

我正在使用 MySQLdb 包。

import MySQLdb
conn = MySQLdb.connect(host="xxx.xxx.xx.xx", user="name", passwd="pwd", db="dbname")

df.to_sql(con = conn, name = 'sample_insert', if_exists = 'append', flavor = 'mysql', index = False)

我使用此查询将其放入我的数据库中。

但我收到错误。

ValueError: database flavor mysql is not supported

我的所有列的数据类型都是 str。

type(df['product_id_x'][0]) = str
type(df['product_id_y'][0]) = str
type(df['count'][0]) = str
type(df['date'][0]) = str

我不想使用 sqlalchemy 或其他包,谁能告诉我这里的错误是什么。提前致谢

最佳答案

pandas 版本 0.19 中不推荐使用 flavor 'mysql'。您必须使用 sqlalchemy 的引擎来创建与数据库的连接。

from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://USER:"+'PASSWORD'+"@localhost/DATABASE")
df.to_sql(con=engine, if_exists='append', index=False)

定义引擎时,需要指定用户、密码、主机和数据库。在您的具体情况下,这应该是这样的:

engine = create_engine("mysql+mysqldb://name:pwd@xxx.xxx.xx.xx/dbname")

关于python - 不支持 pandas dataframe 到 mysql db 错误数据库风格 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com