用pymysql插入mysql数据库值,关于date字段,需要将str转换成date,变换了好几种写法,也查了很久,不是报语法错误,就是插入的日期不对
请教正确的写法
code=1
date='2016-03-01'
insertresult = cur.execute("insert into data values(%d,str_to_date(%s,'%%Y-%%m
-%%d'))" % (code,date))
大家讲道理2017-04-17 17:53:36
Your sql is equivalent to:
insert into data values(1,str_to_date(2016-03-01,'%Y-%m-%d'))
What is needed is '2016-03-01' instead of 2016-03-01
Change to cur.execute("insert into data values('%d',str_to_date(%s,'%%Y-%%m-%%d'))" % (code,date))