sqlalchemy+pandas:错误 'OptionEngine' object has no attribute 'execute','str' object has no attribute '_execute_on_connection'
场景:使用 sqlalchemy+pandas
1. 'OptionEngine' object has no attribute 'execute'
import pandas as pd
from sqlalchemy import create_engine, text engine = create_engine('mysql+pymysql://root:root1234@127.0.0.1:3308/beststock')
sql = "select max(id) as maxrid FROM tspro_stock_basic " df = pd.read_sql(sql, engine)
print(df)
Error: 提示报错信息:
Traceback (most recent call last):
File "C:\Users\Google_he\PycharmProjects\trunk\PyStock\UnitTest\Test_Sqlalchemy.py", line 15, in <module>
df = pd.read_sql(sql, engine)
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 591, in read_sql
return pandas_sql.read_query(
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 1561, in read_query
result = self.execute(*args)
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 1406, in execute
return self.connectable.execution_options().execute(*args, **kwargs)
AttributeError: 'OptionEngine' object has no attribute 'execute'
排查:
查看 pd.read_sql 的 python 源码,原来,需要传递的是一个 connection,
def read_sql(
sql,
con,
index_col: str | list[str] | None = None,
coerce_float: bool = True,
params=None,
parse_dates=None,
columns: list[str] | None = None,
chunksize: int | None = None,
) -> DataFrame | Iterator[DataFrame]:
打印 传递的 engine 类型
print(type(engine))
// <class 'sqlalchemy.engine.base.Engine'>
// 原来对象传递错了,建立 connection,可以通过 engine.connect() 方法 print(type(engine.connect()))
<class 'sqlalchemy.engine.base.Connection'>
所以,需要改成
import pandas as pd
from sqlalchemy import create_engine, text engine = create_engine('mysql+pymysql://root:root1234@127.0.0.1:3308/beststock')
sql = "select max(id) as maxrid FROM tspro_stock_basic " # df = pd.read_sql(text(sql), con=engine.connect()) df = pd.read_sql(sql, engine.connect())
print(df)
2. 'str' object has no attribute '_execute_on_connection'
改成上面的代码后,出现了下面的错误
Traceback (most recent call last):
File "D:\Program Files (x86)\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1410, in execute
meth = statement._execute_on_connection
AttributeError: 'str' object has no attribute '_execute_on_connection' The above exception was the direct cause of the following exception: Traceback (most recent call last):
File "C:\Users\Google_he\PycharmProjects\trunk\PyStock\UnitTest\Test_Sqlalchemy.py", line 16, in <module>
df = pd.read_sql(sql, engine.connect())
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 591, in read_sql
return pandas_sql.read_query(
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 1561, in read_query
result = self.execute(*args)
File "D:\Program Files (x86)\Python39\lib\site-packages\pandas\io\sql.py", line 1406, in execute
return self.connectable.execution_options().execute(*args, **kwargs)
File "D:\Program Files (x86)\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1412, in execute
raise exc.ObjectNotExecutableError(statement) from err
sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: 'select max(id) as maxrid FROM tspro_stock_basic '
继续查看 read_sql() 的源码,关于入参 sql 的注释及示例
sql : str or SQLAlchemy Selectable (select or text object)
SQL query to be executed or a table name.
再次改成下面,还是错误依旧
df = pd.read_sql("select max(id) as maxrid FROM tspro_stock_basic ", engine.connect())
确定是 入参 sql 的类型有问题,再次看到上面有提到的 text object,看来还是需要转换一下
import pandas as pd
from sqlalchemy import create_engine, text engine = create_engine('mysql+pymysql://root:root1234@127.0.0.1:3308/beststock')
sql = "select max(id) as maxrid FROM tspro_stock_basic " df = pd.read_sql(text(sql), con=engine.connect())
print(df) ---- output ----
maxrid
0 5066
sqlalchemy+pandas:错误 'OptionEngine' object has no attribute 'execute','str' object has no attribute '_execute_on_connection'的更多相关文章
- Django2.2报错 AttributeError: 'str' object has no attribute 'decode'
准备将 Django 连接到 MySQL,在命令行输入命令 python manage.py makemigrations 后报错: AttributeError: 'str' object has ...
- Django项目与mysql交互进行数据迁移时报错:AttributeError: 'str' object has no attribute 'decode'
问题描述 Django项目启动,当我们执行命令 python manage.py makemigrations 出现如下错误: File , in last_executed_query query ...
- 解决编码问题:AttributeError: 'str' object has no attribute 'decode'
1. 问题发现: 出现:读取文件,对其进行解码,出现错误,AttributeError: 'str' object has no attribute 'decode' 解释:属性错误,str对象不包含 ...
- Django 运行报异常:AttributeError: 'str' object has no attribute 'get'
Technorati Tags: Python,Django,Web 在使用django.contrib.auth用户机制进行用户的验证.登录.注销操作时,遇到这个异常. 首先是写了一个登录的视图,要 ...
- Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'
pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- python2 'str' object has no attribute 'decode'
'.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...
- python3.x运行的坑:AttributeError: 'str' object has no attribute 'decode'
1.Python3.x和Python2.X版本有一些区别,我遇到了两个问题如下: a.第一个报:mysqlclient 1.3版本不对: 解决办法:注释掉这行即可: b.第二个报:字符集的问题: 报错 ...
- python自动化测试,读取excal数据报"'str' object has no attribute 'items'"问题解决
通过python进行自动化测试,为了方便,对代码和数据进行了分离,此处把测试数据放到了excal表格中.requests.post请求时报"'str' object has no attri ...
- Django项目启动 AttributeError: ‘str‘ object has no attribute ‘decode‘ 问题
Watching for file changes with StatReloader Performing system checks... System check identified no i ...
随机推荐
- .net 下SSE使用demo
所谓SSE,就是浏览器向服务器发送一个HTTP请求,然后服务器不断单向地向浏览器推送"信息"(message).这种信息在格式上很简单,就是"信息"加上前缀&q ...
- 关于 Knex update 语句报错:Undefined binding(s) detected when compiling UPDATE
下图是详细的报错截图,我敢保证前端传递的数据一个不漏,但还是报我没有绑定对应的字段: 官方文档的使用案例基本上都是where 子句在 update 语句之前.但,select 语句的 where 子句 ...
- 基于Vue3的Leaflet基础
1. 概述 Leaflet 是一个开源.轻量并且对移动端友好的交互式地图 JavaScript 库,大小仅仅只有 39 KB, 拥有绝大部分开发者所需要的所有地图特性 Leaflet 的官网为:Lea ...
- PostgreSQL维护年龄的处理
1.错误信息 WARNING: database "postgres" must be vacuumed within 3330803 transactions 最常见的方法是通过 ...
- pip换源和制作虚拟环境操作步骤讲解
目录 一.pip换源及虚拟环境 二.虚拟环境 一.pip换源及虚拟环境 我们Python的强大之处就是有非常多的牛逼的第三方模块,后面的程序员只需要下载第三方模块,然后站在大佬们的肩膀上开发,第三方开 ...
- 28.yield return 语法
一.yield return 该语句可以实现在 foreach 循环中,在其他方法边遍历边输出.如下: class Program { private List<int> list = n ...
- java学习日记20230228-数据类型及加号运算
程序中+使用: 1.两侧是数值型,则相加: 2.一方为字符串,则拼接: 3.运算顺序从做到右: 数据类型 每一种数据都定义了明确的数据类型,在内存中分配了不同大小的内存空间: java数据类型 基本数 ...
- pgsql中物化视图的使用
1.创建物化视图 CREATE MATERIALIZED VIEW "view_xxx" as select * from 表 2.刷新物化视图 refresh material ...
- mysql:数据库加解密查询
解密:SELECT CONVERT (AES_DECRYPT(UNHEX( column_name), '密钥') USING utf8) AS column_name,from table_name ...
- 题解[LuoguP6222]「P6156简单题」加强版
题解[LuoguP6222]「P6156简单题」加强版 加强版很好地体现了这个题的真正价值.(当然是指卡常 本题解给出了本题更详尽的推倒导和思考过程,思路与 CYJian 的类似,具体式子的个别地方换 ...