关于 pandas.to_sql 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43136121/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:19:04  来源:igfitidea点击:

questions about pandas.to_sql

pandasmysql-python

提问by user7796650

I have a question about how to save a dataframe to my local mysql.

我有一个关于如何将数据框保存到本地 mysql 的问题。

import MySQLdb
import pandas as pd
conn=MySQLdb.connect(host="localhost",user='root',passwd="matt123",db="ada")
df=pd.DataFrame(['A','B'],columns=['new_tablecol'])
df.to_sql(name='new_table',con=conn,if_exists='append')

after typing this code , it says

输入此代码后,它说

pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting

I am confused about this.I can query and create table . but I can't save this dataframe.

我对此感到困惑。我可以查询和创建表。但我无法保存此数据框。

回答by James

Your way is not supported anymore.

不再支持您的方式。

con :SQLAlchemy engine or DBAPI2 connection (legacy mode)

Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

flavor :‘sqlite', default None

Deprecated since version 0.19.0: ‘sqlite' is the only supported option if SQLAlchemy is not used.

con :SQLAlchemy 引擎或 DBAPI2 连接(传统模式)

使用 SQLAlchemy 可以使用该库支持的任何数据库。如果是 DBAPI2 对象,则仅支持 sqlite3。

风味:'sqlite',默认无

0.19.0 版后已弃用:如果不使用 SQLAlchemy,'sqlite' 是唯一受支持的选项。

pandas.DataFrame.to_sql

pandas.DataFrame.to_sql

Try this?

尝试这个?

from sqlalchemy import create_engine
import pandas as pd


engine = create_engine("mysql://root:matt123@localhost/ada")
con = engine.connect()
df = pd.DataFrame(['A','B'],columns=['new_tablecol'])
df.to_sql(name='new_table',con=con,if_exists='append')
con.close()

Syntax is:

语法是:

engine = create_engine("mysql://USER:PASSWORD@HOST/DATABASE")

More information about sqlalchemycan be found here

sqlalchemy可以在此处找到有关更多信息