尝试使用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'
答案 0 :(得分:63)
使用引擎代替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)
我不明白为什么当我昨天尝试这个时它给了我早先的错误
答案 1 :(得分:6)
或者,使用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)
答案 2 :(得分:6)
使用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.
"""
答案 3 :(得分:0)
我知道在问题的标题中包含了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。
答案 4 :(得分:0)
该问题的快速解决方案是在脚本中包含以下行:
pd.io.sql._SQLALCHEMY_INSTALLED = True
原因是因为to_sql
调用pandasSQL_builder
本身会调用_is_sqlalchemy_connectable
,后者检查是否已安装sqlalchemy。但是由于某种原因,即使安装了sqlalchemy,该函数似乎也认为不是。我正在使用熊猫0.24.2。