我正在尝试使用pyodbc将datetime值插入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.
删除datetime到字符串的转换并改为使用参数:
还可以使用datetime.strftime()以所需的方式将datetime对象转换为字符串。str(datetime)是个坏主意。
currenttime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") cursor.execute("""insert into currentvalue(value1,currentdatetime) values(55,"""+ currenttime+")")
相关问题 更多 >
编程相关推荐