pandas dataframe到mysql db错误数据库风味mysql不支持 [英] pandas dataframe to mysql db error database flavor mysql is not supported
问题描述
我在pandas数据框df中有一张桌子.
I have a table in 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数据库中.
i want to save this table in mysql database.
我正在使用MySQLdb软件包.
i am using MySQLdb package.
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)
我使用此查询将其放入我的数据库中.
i used this query to put it in my db.
但是我出错了.
ValueError: database flavor mysql is not supported
所有列的数据类型均为str.
my datatype is str for all the columns.
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或其他软件包,任何人都可以在这里说出什么错误. 预先感谢
i don't want to use sqlalchemy or other packages, can anyone tell what's the error here. Thanks in advance
推荐答案
pandas版本0.19中不推荐使用mysql版本.您必须使用sqlalchemy的引擎来创建与数据库的连接.
The flavor 'mysql' is deprecated in pandas version 0.19. You have to use the engine from sqlalchemy to create the connection with the database.
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://USER:"+'PASSWORD'+"@localhost/DATABASE")
df.to_sql(con=engine, if_exists='append', index=False)
定义引擎时,需要指定用户,密码,主机和数据库.在您的特定情况下,它应该类似于:
When defining your engine, you need to specify your user, password, host and database. In your specific case, this should look like:
engine = create_engine("mysql+mysqldb://name:pwd@xxx.xxx.xx.xx/dbname")
这篇关于pandas dataframe到mysql db错误数据库风味mysql不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!