为什么我要获得此Python MS SQL Server错误?
当尝试从熊猫数据帧写入MS SQL Server时,我一直在下面遇到相同的错误。我已经检查了所有数据是否与表上的数据类型匹配。
:
posting_date | source | readtype | pptagid | eTagid_plate | Equipid | 代理 | ement_plaza | 00 | : | 22- | 00 | : | 00 | ID |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toll_class | : | 00 | exit_plaza | miles | exit_date | 2022-06-06-06-06-06 | entry_date | toll_amount | 数据 | 示例 06-04 21:50:25 | 5 | 0.00 | 31.95 | 111111111111111111111 |
2022-06-06 00:00:00 | EZPass | Transponder | 777777777 | 11111111111 | 5363 | DelDOT | NaN | NaN | D95 | 2022-06-03 03:08:21 | 5 | 0.00 | 9.00 | 111111111111111111111 |
2022-06-06 00:00:00 | EZPASS | 应答器 | 777777771111111111 | : | 5363 | NJTP | 1 | 2022-06-03 03:27:38 | 2 | 2022-06-06-06-03 03 | 56 5 | 0.00 | : | : |
39 | 39 | 39 | : | 1111111111 | 5362 | KTA | WICHITA:I-135 I-235 47th ST | 15:21:21 | 入口 | 2022-06-06-04 15:47:57 | 5 | 0.00 5.05 | 5.05 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 | 拉蒂亚希亚 |
Southern | 2022-06-04 | Transponder | 777777777 | 11111111111 | 5357 | OTA | WR-BGCBN | 2022-06-05 02:05:25 | WR-STLINE | 2022-06-05 02:38:17 | 5 | 0.00 | 9.20 | 111111111111111111111 |
2022-06-06 00:00:00 | ELITE | Transponder | 777777777 | 11111111111 | 5355 | OTA | HEB-NWCLML | 2022-06-03 07:10:36 | HEB-NWCLML | 2022-06-03 07:10:36 | 5 | 0.00 | 3.95 | 11111111111111111111 |
表结构:
[PostingDate] datetime,
[Source] varchar(50),
[ReadType] varchar(50),
[PPTagID] varchar(10),
[ETagID_Plate] varchar(12),
[EquipID] varchar(4),
[Agency] varchar(10),
[Entry_Plaza] varchar(50),
[Entry_Date] datetime,
[Exit_Plaza] varchar(50),
[Exit_Date] datetime,
[Toll_Class] varchar(5),
[Miles] numeric(18, 2),
[Toll_Amount] numeric(18, 2),
[ID] varchar(50)
Python代码:
for row in df.itertuples():
try:
MsExe.execute("""
INSERT INTO test_tolls_table_temp (PostingDate, Source, ReadType, PPTagID, ETagID_Plate, EquipID, Agency, Entry_Plaza, Entry_Date, Exit_Plaza, Exit_Date, Toll_Class, Miles, Toll_Amount, ID)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15])
except Exception as exception:
print(exception)
break
错误:错误:
('42000','[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server] 传入的表格数据流(TDS)远程过程调用(RPC) 协议流不正确。参数9(“”):提供的值为 不是数据类型浮点的有效实例。检查源数据的源数据 无效的值。无效值的一个示例是数字类型的数据 比例大于精度。 (8023)(sqlexecdirectw)')
I keep getting the same error below when trying to write from a pandas data frame to MS sql server. I have triple checked that all my data matches the data types on the table.
Sample Data:
POSTING_DATE | SOURCE | READTYPE | PPTAGID | ETAGID_PLATE | EQUIPID | AGENCY | ENTRY_PLAZA | ENTRY_DATE | EXIT_PLAZA | EXIT_DATE | TOLL_CLASS | MILES | TOLL_AMOUNT | ID |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2022-06-06 00:00:00 | ELITE | Transponder | 777777777 | 11111111111 | 5363 | KTA | Eastern Entrance | 2022-06-04 17:52:39 | Southern Entrance | 2022-06-04 21:50:25 | 5 | 0.00 | 31.95 | 111111111111111111111 |
2022-06-06 00:00:00 | EZPass | Transponder | 777777777 | 11111111111 | 5363 | DelDOT | NaN | NaN | D95 | 2022-06-03 03:08:21 | 5 | 0.00 | 9.00 | 111111111111111111111 |
2022-06-06 00:00:00 | EZPass | Transponder | 777777777 | 11111111111 | 5363 | NJTP | 1 | 2022-06-03 03:27:38 | 2 | 2022-06-03 03:39:56 | 5 | 0.00 | 4.60 | 111111111111111111111 |
2022-06-06 00:00:00 | ELITE | Transponder | 777777777 | 11111111111 | 5362 | KTA | Wichita: I-135 I-235 47th St | 2022-06-04 15:21:21 | Southern Entrance | 2022-06-04 15:47:57 | 5 | 0.00 | 5.05 | 111111111111111111111 |
2022-06-06 00:00:00 | ELITE | Transponder | 777777777 | 11111111111 | 5357 | OTA | WR-BGCBN | 2022-06-05 02:05:25 | WR-STLINE | 2022-06-05 02:38:17 | 5 | 0.00 | 9.20 | 111111111111111111111 |
2022-06-06 00:00:00 | ELITE | Transponder | 777777777 | 11111111111 | 5355 | OTA | HEB-NWCLML | 2022-06-03 07:10:36 | HEB-NWCLML | 2022-06-03 07:10:36 | 5 | 0.00 | 3.95 | 111111111111111111111 |
Table Structure:
[PostingDate] datetime,
[Source] varchar(50),
[ReadType] varchar(50),
[PPTagID] varchar(10),
[ETagID_Plate] varchar(12),
[EquipID] varchar(4),
[Agency] varchar(10),
[Entry_Plaza] varchar(50),
[Entry_Date] datetime,
[Exit_Plaza] varchar(50),
[Exit_Date] datetime,
[Toll_Class] varchar(5),
[Miles] numeric(18, 2),
[Toll_Amount] numeric(18, 2),
[ID] varchar(50)
Python Code:
for row in df.itertuples():
try:
MsExe.execute("""
INSERT INTO test_tolls_table_temp (PostingDate, Source, ReadType, PPTagID, ETagID_Plate, EquipID, Agency, Entry_Plaza, Entry_Date, Exit_Plaza, Exit_Date, Toll_Class, Miles, Toll_Amount, ID)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15])
except Exception as exception:
print(exception)
break
Error:
('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server] The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 9 (""): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision. (8023) (SQLExecDirectW)')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
都不需要通过像
行[1]
,行[2]
...代码>行[15] , nor a loop但 直接使用
执行人
(DML操作的性能比execute
),例如Neither need to specify each column individually by like
row[1]
,row[2]
...row[15]
, nor a loopbut use directly
executemany
(which's also more performant for DML operations thanexecute
) such as