正在try 使用to_sql
将pandas dataframe写入MySQL表.之前一直在使用flavor='mysql'
,但它将在future 贬值,并希望开始过渡到使用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"从sqlite_主机 Select 名称"上执行失败
为什么它看起来像是在try 使用sqlite?sqlalchemy与mysql,特别是mysql的连接的正确用法是什么.连接器?
我还try 将引擎作为连接传入,这给了我一个引用no cursor对象的错误.
data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
>>AttributeError: 'Engine' object has no attribute 'cursor'