gpt4 book ai didi

python - 在 Ms-Sql 中插入 DateTime 字段

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:54 24 4
gpt4 key购买 nike

我想在 Ms-SQL 数据库中插入一个日期。我该怎么做?

这是我正在做的:-

a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
data = {'AWB_Number':'1','Weight':'1','Length':'1','Height':'1','Width':'1','Customer_Name':'Naaptol','Scan_Time': a,'Series_Flag':'Others'}

data = (
data['AWB_Number'], data['Weight'], data['Length'], data['Height'],
data['Width'], data['Customer_Name'], data['Scan_Time'] ,data['Series_Flag']
)

print data


con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (aramex_dsn, aramex_user, aramex_password, aramex_database)
cnxn = pyodbc.connect(con_string)

cursor = cnxn.cursor()

cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
cnxn.commit()

cnxn.close()

它返回一个错误提示

Traceback (most recent call last):
File "tests.py", line 39, in <module>
cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near '09'. (102) (SQLExecDirectW)")

问题是什么?

以下是数据库结构:-

    AWB_Number = models.CharField(max_length = 255)
Weight = models.CharField(max_length = 255)
Length = models.CharField(max_length = 255)
Width = models.CharField(max_length = 255)
Height = models.CharField(max_length = 255)
Customer_Name = models.CharField(max_length = 255)
Scan_Time = models.DateTimeField(db_index = True)
Series_Flag = models.CharField(max_length = 255)

最佳答案

如我所见here ,我认为对于数据库中的日期时间值,你必须有一个 datetime.datetime 对象,而不是一个字符串。所以,只需更换

a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")

通过

a = datetime.datetime.now()

关于python - 在 Ms-Sql 中插入 DateTime 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25304683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com