pandas 数据框到 mysql db 错误数据库风格 mysql 不受支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40842898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas dataframe to mysql db error database flavor mysql is not supported
提问by Shubham R
I have a table in pandas dataframe df.
我在 Pandas 数据框 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
i want to save this table in mysql database.
我想将此表保存在 mysql 数据库中。
i am using MySQLdb package.
我正在使用 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)
i used this query to put it in my db.
我用这个查询把它放在我的数据库中。
but i am getting error.
但我收到错误。
ValueError: database flavor mysql is not supported
my datatype is str for all the columns.
我的所有列的数据类型都是 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
i don't want to use sqlalchemy or other packages, can anyone tell what's the error here. Thanks in advance
我不想使用 sqlalchemy 或其他软件包,谁能告诉我这里的错误是什么。提前致谢
回答by Marine
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.
pandas 0.19 版本中不推荐使用“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)
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:[email protected]/dbname")
回答by gold_cy
It appears the option you are using for flavor
no longer exists. A brief look at the docs confirms this:
看来您正在使用的选项flavor
不再存在。对文档的简要介绍证实了这一点:
flavor: ‘sqlite', default None
风味:'sqlite',默认无
DEPRECATED: this parameter will be removed in a future version, as ‘sqlite' is the only supported option if SQLAlchemy is not installed.
已弃用:此参数将在未来版本中删除,因为如果未安装 SQLAlchemy,'sqlite' 是唯一受支持的选项。