Home  >  Q&A  >  body text

python的pymysql如何插入date类型的字段

用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))

ringa_leeringa_lee2742 days ago1017

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理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))

    reply
    0
  • Cancelreply