我正在尝试使用pyodbc将日期时间值插入到MS sql Server表中.
如果我手动执行,例如:
如果我手动执行,例如:
cursor.execute("""insert into currentvalue(value1,currentdatetime) values(55,'2014-06-27 16:42:48.533')""")
我没有任何问题,但是当我尝试做的时候:
currenttime = str(datetime.datetime.now()) cursor.execute("""insert into currentvalue(value1,currentdatetime) values(55,"""+ currenttime+")")
我收到了这个错误:
sql server Incorrect Syntax near ’07’ which i think is the number after the date and starting the time.
我也试过这个:
currenttime = "'"+str(datetime.datetime.now())+"'"
现在出现这个错误:
Conversion Failed when converting date and/or time from character string.
解决方法
删除日期时间到字符串转换,而不是使用参数:
.... cursor.execute("insert into currentvalue (value1,currentdatetime) values(?,?)",(value1,datetime.datetime.now())) ....