pandas写入mysql_使用SQLAlchemy使用pandas写入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: Execution failed on sql 'SELECT name FROM sqlite_master

WHERE type='table' AND name=?;': Wrong number of arguments during

string formatting

为什么它看起来像是在尝试使用sqlite?对于mysql,特别是mysql.connector,正确使用sqlalchemy连接是什么?

我还尝试将引擎作为连接传入,这给了我一个引用no cursor对象的错误。data.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)

>>AttributeError: 'Engine' object has no attribute 'cursor'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值