Inserting a Python datetime.datetime object into MySQL


Question

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?

I have tried:

now = datetime.datetime(2009,5,5)

cursor.execute("INSERT INTO table
(name, id, datecolumn) VALUES (%s, %s
, %s)",("name", 4,now))

I am getting an error as: "TypeError: not all arguments converted during string formatting" What should I use instead of %s?

1
105
11/15/2015 8:44:12 AM

For a time field, use:

import time    
time.strftime('%Y-%m-%d %H:%M:%S')

I think strftime also applies to datetime.

177
11/15/2015 8:53:45 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon