加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

使用pyodbc将日期时间插入MS SQL表

发布时间:2020-12-12 06:49:14 所属栏目:MsSql教程 来源:网络整理
导读:我正在尝试使用pyodbc将日期时间值插入到MS SQL Server表中. 如果我手动执行,例如: cursor.execute("""insert into currentvalue(value1,currentdatetime) values(55,'2014-06-27 16:42:48.533')""") 我没有任何问题,但是当我尝试做的时候: currenttime = s
我正在尝试使用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()))
....

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读