Inserting a Python datetime.datetime object into MySQL

后端 未结 7 1281
Happy的楠姐
Happy的楠姐 2020-11-27 11:26

I have a date column in a MySQL table. I want to insert a datetime.datetime() object into this column. What should I be using in the execute statement?

7条回答
  •  不知归路
    2020-11-27 11:51

    Try using now.date() to get a Date object rather than a DateTime.

    If that doesn't work, then converting that to a string should work:

    now = datetime.datetime(2009,5,5)
    str_now = now.date().isoformat()
    cursor.execute('INSERT INTO table (name, id, datecolumn) VALUES (%s,%s,%s)', ('name',4,str_now))
    

提交回复
热议问题