在Python中插入一个Python datetime.datetime对象 [英] Inserting a Python datetime.datetime object into MySQL
本文介绍了在Python中插入一个Python datetime.datetime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
datetime.datetime()
对象。在执行语句中应该使用什么? 我尝试过:
now = datetime.datetime(2009,5,5)
cursor.execute(INSERT INTO table
(name,id,datecolumn)VALUES(%s,%s
,%s),(name,4,now))
得到一个错误:TypeError:不是字符串格式化期间转换的所有参数
应该使用什么而不是%s
?
解决方案
对于时间字段,请使用:
import time
time.strftime('%Y-%m-%d%H:%M:%S')
我认为strftime也适用于datetime。
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
?
解决方案
For a time field, use:
import time
time.strftime('%Y-%m-%d %H:%M:%S')
I think strftime also applies to datetime.
这篇关于在Python中插入一个Python datetime.datetime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文