【发布时间】:2019-12-03 00:30:12
【问题描述】:
正在尝试更新 Microsoft Server SQL 表列。我正在使用 pyodbc 库来这样做。不知何故,我收到一个错误,似乎在抱怨日期匹配条件。
注意:日期时间列(在数据库中)的 SQL Server 字段类型是“日期”
Dataframe(用于更新DB表)摘要:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100000 entries, 0 to 99999
Data columns (total 4 columns):
SID 100000 non-null object
prod 100000 non-null object
Response Date 100000 non-null object
RId 100000 non-null object
dtypes: int64(1), object(4)
memory usage: 8.4+ MB
数据框“数据”日期字段:
data['Response Date'] = pd.to_datetime(data['Response Date'], infer_datetime_format = True).dt.date
data['Response Date']
Out[139]:
0 2019-11-25
1 2019-11-25
2 2019-11-25
3 2019-11-25
4 2019-11-25
99995 2019-05-21
99996 2019-05-21
99997 2019-05-21
99998 2019-05-21
99999 2019-05-21
Name: Response Date, Length: 100000, dtype: object
用于执行更新的语句
cursor.execute("Update dbo.dtable set dbprod = '%s' where dbrid = '%s'and date_time = '%s' and dbsid = '%s'" % (data['prod'], data['RId'], data['Response Date'], data['SID']))
错误
DataError: ('22007', '[22007] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]从字符串转换日期和/或时间时转换失败。(241) (SQLExecDirectW)')
【问题讨论】:
标签: sql-server pandas datetime sql-update pyodbc