完美解决sqlalchemy.exc.ObjectNotExecutableError: Not an executable object

报错的程序如下:

from sqlalchemy import  create_engine
import pandas as pd
engine=create_engine('mysql+pymysql://root:123456@localhost/gpt') #建立mysql引擎
df=pd.read_sql('select * from gpt-last',con=engine.connect()) #读取mysql数据至df

报错sqlalchemy.exc.ObjectNotExecutableError: Not an executable object

然后查询别人的回答,说要import text并且将sql语句字符串转换为可执行语句,故更改代码如下:

import pandas as pd
from sqlalchemy import  create_engine,text

engine=create_engine('mysql+pymysql://root:123456@localhost/gpt') #建立mysql引擎
df=pd.read_sql(text('select * from gpt-last'),con=engine.connect())#读取mysql数据至df

结果报新的错sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError)(1064, "You have an error in your SQL syntax;)提示我的sql语句有错误,但是这条简单的语句怎么会出错?

思考再三,是不是我的表名中含有特殊字符"-"导致出错了?抱着试一试的态度,将mysql中的表名中和sql语句中的"-"都删掉了,如下,果然程序运行成功!!!

import pandas as pd
from sqlalchemy import  create_engine,text

engine=create_engine('mysql+pymysql://root:123456@localhost/gpt') #建立mysql引擎
df=pd.read_sql(text('select * from gptlast'),con=engine.connect())#读取mysql数据至df

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值