首页 > 数据库技术 > 详细

python3 mysql 时间参数错误

时间:2018-12-12 18:33:32      阅读:170      评论:0      收藏:0      [点我收藏+]

一、环境

    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)

 

python3 mysql 时间参数错误

原文:https://www.cnblogs.com/medivhxu/p/10109833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!