Inserting Python datetime.datetime Objects into MySQL
Q: How can I insert a datetime.datetime object into a MySQL date column?
A: To insert a datetime.datetime object into a MySQL date column, you need to use an appropriate format that MySQL can recognize. The following steps outline the process:
import time formatted_datetime = time.strftime('%Y-%m-%d %H:%M:%S')
cursor.execute("INSERT INTO table (name, id, datecolumn) VALUES (%s, %s, %s)", ("name", 4, formatted_datetime))
Note that you should enclose the formatted_datetime in single quotes because you are inserting a string into the database.
This method will successfully insert the datetime.datetime object into the MySQL date column.
The above is the detailed content of How to Insert Python\'s datetime.datetime Objects into a MySQL DATE Column?. For more information, please follow other related articles on the PHP Chinese website!