我正在尝试使用 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()))
....
关于python - 使用 pyodbc 将日期时间插入到 MS SQL 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458124/