python-使用SQLAlchemy to_sq使用熊猫写入MySQL数据库
尝试使用to_sql将pandas数据帧写入MySQL表。以前使用过flavor='mysql',但是将来会贬值,希望开始过渡到使用SQLAlchemy引擎。
样例代码:
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False)
cnx = engine.raw_connection()
data = pd.read_sql('SELECT * FROM sample_table', cnx)
data.to_sql(name='sample_table2', con=cnx, if_exists = 'append', index=False)
读取工作正常,但to_sql出现错误:
DatabaseError:对sql'SELECT name from sqlite_master的执行失败 WHERE type ='table'AND name = ?;':期间的参数数量错误 字符串格式
为什么看起来要使用sqlite? sqlalchemy与mysql特别是mysql.connector的正确使用是什么?
我也尝试将引擎作为连接传递,这给了我一个错误,该错误没有引用任何游标对象。
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
>>AttributeError: 'Engine' object has no attribute 'cursor'
5个解决方案
67 votes
使用引擎代替raw_connection()的工作原理是:
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]', echo=False)
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
不清楚为什么我昨天尝试这样做时会给我更早的错误。
AsAP_Sherb answered 2020-07-07T12:22:25Z
8 votes
或者,使用pymysql封装...
import pymysql
from sqlalchemy import create_engine
cnx = create_engine('mysql+pymysql://[user]:[pass]@[host]:[port]/[schema]', echo=False)
data = pd.read_sql('SELECT * FROM sample_table', cnx)
data.to_sql(name='sample_table2', con=cnx, if_exists = 'append', index=False)
openwonk answered 2020-07-07T12:22:45Z
7 votes
使用pymysql和sqlalchemy,这适用于Pandas v0.22:
import pandas as pd
import pymysql
from sqlalchemy import create_engine
user = 'yourUserName'
passw = 'password'
host = 'hostName' # either localhost or ip e.g. '172.17.0.2' or hostname address
port = 3306
database = 'dataBaseName'
mydb = create_engine('mysql+pymysql://' + user + ':' + passw + '@' + host + ':' + str(port) + '/' + database , echo=False)
directory = r'directoryLocation' # path of csv file
csvFileName = 'something.csv'
df = pd.read_csv(os.path.join(directory, csvFileName ))
df.to_sql(name=csvFileName[:-4], con=mydb, if_exists = 'replace', index=False)
"""
if_exists: {'fail', 'replace', 'append'}, default 'fail'
fail: If table exists, do nothing.
replace: If table exists, drop it, recreate it, and insert data.
append: If table exists, insert data. Create if does not exist.
"""
DougR answered 2020-07-07T12:23:07Z
1 votes
我知道在问题的标题中包括单词SQLAlchemy,但是我在问题和答案中看到需要导入pymysql或mysql.connector,也可以使用pymysql来完成这项工作,而无需调用SQLAlchemy。
import pymysql
user = 'root'
passw = 'my-secret-pw-for-mysql-12ud' # In previous posts variable "pass"
host = '172.17.0.2'
port = 3306
database = 'sample_table' # In previous posts similar to "schema"
conn = pymysql.connect(host=host,
port=port,
user=user,
passwd=passw,
db=database)
data.to_sql(name=database, con=conn, if_exists = 'append', index=False, flavor = 'mysql')
我认为这种解决方案虽然不使用SQLAlchemy,但会很好。
Rafael Valero answered 2020-07-07T12:23:32Z
-1 votes
该问题的快速解决方案是在脚本中包含以下行:
pd.io.sql._SQLALCHEMY_INSTALLED = True
原因是因为to_sql会呼叫pandasSQL_builder,而本身会呼叫_is_sqlalchemy_connectable,后者会检查是否已安装sqlalchemy。 但是由于某种原因,即使安装了sqlalchemy,该函数似乎也认为不是。 我正在使用熊猫0.24.2。
pedrostrusso answered 2020-07-07T12:23:57Z