使用sqlalchemy,mysql和pandas读取框架 [英] read frame with sqlalchemy, mysql and pandas

查看:217
本文介绍了使用sqlalchemy,mysql和pandas读取框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到mysql数据库, 与选项1配合正常:

I am trying to connect to a mysql database, works fine with Option 1:

from sqlalchemy import create_engine
engine = create_engine('mysql://root:root@localhost/lend', echo=True)
cnx = engine.connect()
x = cnx.execute("SELECT * FROM user")

但在此处细分:

from pandas.io import sql
xx = sql.read_frame("SELECT * FROM user", cnx)
cnx.close()

使用

AttributeError:连接"对象没有属性回退"

AttributeError: 'Connection' object has no attribute 'rollback'

推荐答案

您需要具有原始数据库连接,而不是 engine.raw_connection() engine.connect().connection :

You need to have a raw database connection, and not an instance of Connection. In order to get it call either engine.raw_connection() or engine.connect().connection:

from pandas.io import sql
#cnx = engine.connect().connection # option-1
cnx = engine.raw_connection() # option-2
xx = sql.read_frame("SELECT * FROM user", cnx)
cnx.close()

这篇关于使用sqlalchemy,mysql和pandas读取框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆