可以再数据库查询界面执行的SQL,一直报错
unsupported format character 'Y' (0x59) at index 61
SQL如下:
data=pd.read_sql_query(sql="""select
date_format(create_time,'%Y-%m') as mon
,count(distinct order_id) as ord_cnt
from prod.order_info
where date_format(create_time,'%Y-%m') between '2022-01' and '2022-12'
group by date_format(create_time,'%Y-%m')
order by date_format(create_time,'%Y-%m')"""
,con=con,index_col=['mon'])
'Y'那里前面是有%的,才想起来%在Python里是有特殊含义的字符,所以要用其他方式处理掉,比较简单的方法是用 '%%' 代替 '%',这样就是一个单纯的百分号了
data=pd.read_sql_query(sql="""select
date_format(create_time,'%%Y-%%m') as mon
,count(distinct order_id) as ord_cnt
from prod.order_info
where date_format(create_time,'%%Y-%%m') between '2022-01' and '2022-12'
group by date_format(create_time,'%%Y-%%m')
order by date_format(create_time,'%%Y-%%m')"""
,con=con,index_col=['mon'])
正常执行成功!
文章来源:https://www.toymoban.com/news/detail-548976.html
文章来源地址https://www.toymoban.com/news/detail-548976.html
到了这里,关于【Python】执行SQL报错的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!