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