如何使用pymssql插入datatime对象?我知道SQL Server表需要一个datetime对象,比如说在位置3。我试过这三种方法:
cursor.execute("INSERT INTO MyTable VALUES(1, 'Having Trouble', datetime.datetime.now())")
cursor.execute("INSERT INTO MyTable VALUES(1, 'Having Trouble', 20130410)")
cursor.execute("INSERT INTO MyTable VALUES(1, 'Having Trouble', '20130410')")
cursor.execute("INSERT INTO MyTable VALUES(1, 'Having Trouble', GETDATE())")
每次我都会得到同样的错误:
OperationalError: (241, 'Conversion failed when converting date and/or time from character string.DB-Lib error message 241, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n')
我搜遍了那里的小文件,反复搜遍了。
您试图插入一个未格式化为日期的字符串(datetime.datetime.now(),20130410,'20130410',GETDATE()),因此sql server无法从中分析日期。。。
所以试试这个。。。
您可以使用以下代码:
试试这个:
此时间戳格式可由MS SQL SERVER转换,并可在PymSQL中用于插入datetime类型的对象
相关问题 更多 >
编程相关推荐