[英]Why do I get this python MS sql server Error?
I keep getting the same error below when trying to write from a pandas data frame to MS sql server.尝试从 pandas 数据帧写入 MS sql 服务器时,我在下面不断收到相同的错误。 I have triple checked that all my data matches the data types on the table.我已经三次检查我的所有数据是否与表上的数据类型匹配。
Sample Data:样本数据:
POSTING_DATE发布日期 | SOURCE资源 | READTYPE读类型 | PPTAGID PPTAGID | ETAGID_PLATE ETAGID_PLATE | EQUIPID设备 | AGENCY机构 | ENTRY_PLAZA ENTRY_PLAZA | ENTRY_DATE ENTRY_DATE | EXIT_PLAZA EXIT_PLAZA | EXIT_DATE EXIT_DATE | TOLL_CLASS TOLL_CLASS | MILES英里 | TOLL_AMOUNT TOLL_AMOUNT | ID ID |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2022-06-06 00:00:00 2022-06-06 00:00:00 | ELITE精英 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5363 5363 | KTA KTA | Eastern Entrance东入口 | 2022-06-04 17:52:39 2022-06-04 17:52:39 | Southern Entrance南入口 | 2022-06-04 21:50:25 2022-06-04 21:50:25 | 5 5 | 0.00 0.00 | 31.95 31.95 | 111111111111111111111 111111111111111111111 |
2022-06-06 00:00:00 2022-06-06 00:00:00 | EZPass易通票 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5363 5363 | DelDOT德尔点 | NaN钠 | NaN钠 | D95 D95 | 2022-06-03 03:08:21 2022-06-03 03:08:21 | 5 5 | 0.00 0.00 | 9.00 9.00 | 111111111111111111111 111111111111111111111 |
2022-06-06 00:00:00 2022-06-06 00:00:00 | EZPass易通票 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5363 5363 | NJTP新泽西州 | 1 1 | 2022-06-03 03:27:38 2022-06-03 03:27:38 | 2 2 | 2022-06-03 03:39:56 2022-06-03 03:39:56 | 5 5 | 0.00 0.00 | 4.60 4.60 | 111111111111111111111 111111111111111111111 |
2022-06-06 00:00:00 2022-06-06 00:00:00 | ELITE精英 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5362 5362 | KTA KTA | Wichita: I-135 I-235 47th St威奇托:I-135 I-235 47th St | 2022-06-04 15:21:21 2022-06-04 15:21:21 | Southern Entrance南入口 | 2022-06-04 15:47:57 2022-06-04 15:47:57 | 5 5 | 0.00 0.00 | 5.05 5.05 | 111111111111111111111 111111111111111111111 |
2022-06-06 00:00:00 2022-06-06 00:00:00 | ELITE精英 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5357 5357 | OTA OTA | WR-BGCBN WR-BGCBN | 2022-06-05 02:05:25 2022-06-05 02:05:25 | WR-STLINE WR-STLINE | 2022-06-05 02:38:17 2022-06-05 02:38:17 | 5 5 | 0.00 0.00 | 9.20 9.20 | 111111111111111111111 111111111111111111111 |
2022-06-06 00:00:00 2022-06-06 00:00:00 | ELITE精英 | Transponder转发器 | 777777777 777777777 | 11111111111 11111111111 | 5355 5355 | OTA OTA | HEB-NWCLML希伯来语-NWCLML | 2022-06-03 07:10:36 2022-06-03 07:10:36 | HEB-NWCLML希伯来语-NWCLML | 2022-06-03 07:10:36 2022-06-03 07:10:36 | 5 5 | 0.00 0.00 | 3.95 3.95 | 111111111111111111111 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)')
Neither need to specify each column individually by like row[1]
, row[2]
... row[15]
, nor a loop既不需要像row[1]
, row[2]
... row[15]
单独指定每一列,也不需要循环
but use directly executemany
(which's also more performant for DML operations than execute
) such as但直接使用executemany
(这对于 DML 操作也比execute
更高效),例如
import pyodbc
import pandas as pd
con = pyodbc.connect(
"DRIVER=ODBC Driver xx for SQL Server;"
"SERVER=<IP>,<some number>;"
"DATABASE=DB123;"
"Trusted_Connection=yes;"
)
data = pd.read_csv(pathToFile,delimiter= ';') # is just a sample delimiter representation, you should replace with yours
df = data.values.tolist()
MsExe=con.cursor()
try:
MsExe.fast_executemany = True
MsExe.executemany("""
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",df)
except Exception as exception:
print(exception)
MsExe.close()
con.commit()
con.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.