python3 mysql 时间参数错误

一、环境

    mac OS + python3.6 + pymysql

二、执行

1、语句

select count(user_id)
from chezhubangapppp.yfq_user
where register_dt between '2018-11-01' and '2018-12-01';

 

写成这样直接会报错,说between and之间必须是时间类型

2、修改后

"select count(user_id) from chezhubangapppp.yfq_user where register_dt between str_to_date('%s', '%Y-%m-%d') and str_to_date('%s', '%Y-%m-%d');" % ('2018-11-01', '2018-12-01')

报错

ValueError: unsupported format character 'Y' (0x59) at index 98

为啥?因为python执行的sql中存在类似DATE_FORMAT(CREATE_TIME, ‘%Y-%m-%d’) 的写法,与其中%Y与python的参数%s冲突

三、结论

修改后的结果

select count(user_id) from chezhubangapppp.yfq_user where register_dt between str_to_date('%s', '%%Y-%%m-%%d') and str_to_date('%s', '%%Y-%%m-%%d')" % ('2018-11-01', '2018-12-01')

 

作者:medivhxu

出处:https://www.cnblogs.com/medivhxu/p/10109833.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   Medivh_Xu  阅读(285)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题