为什么我要获得此Python MS SQL Server错误?

发布于 2025-02-05 15:26:21 字数 3651 浏览 2 评论 0原文

当尝试从熊猫数据帧写入MS SQL Server时,我一直在下面遇到相同的错误。我已经检查了所有数据是否与表上的数据类型匹配。

posting_datesourcereadtypepptagideTagid_plateEquipid代理ement_plaza00:22-00:00ID
toll_class:00exit_plazamilesexit_date2022-06-06-06-06-06entry_datetoll_amount数据示例 06-04 21:50:2550.0031.95111111111111111111111
2022-06-06 00:00:00EZPassTransponder777777777111111111115363DelDOTNaNNaND952022-06-03 03:08:2150.009.00111111111111111111111
2022-06-06 00:00:00EZPASS应答器777777771111111111:5363NJTP12022-06-03 03:27:3822022-06-06-06-03 0356 50.00::
393939:11111111115362KTAWICHITA:I-135 I-235 47th ST15:21:21入口2022-06-06-04 15:47:5750.00 5.055.05 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111拉蒂亚希亚
Southern2022-06-04Transponder777777777111111111115357OTAWR-BGCBN2022-06-05 02:05:25WR-STLINE2022-06-05 02:38:1750.009.20111111111111111111111
2022-06-06 00:00:00ELITETransponder777777777111111111115355OTAHEB-NWCLML2022-06-03 07:10:36HEB-NWCLML2022-06-03 07:10:3650.003.9511111111111111111111

表结构:

[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_DATESOURCEREADTYPEPPTAGIDETAGID_PLATEEQUIPIDAGENCYENTRY_PLAZAENTRY_DATEEXIT_PLAZAEXIT_DATETOLL_CLASSMILESTOLL_AMOUNTID
2022-06-06 00:00:00ELITETransponder777777777111111111115363KTAEastern Entrance2022-06-04 17:52:39Southern Entrance2022-06-04 21:50:2550.0031.95111111111111111111111
2022-06-06 00:00:00EZPassTransponder777777777111111111115363DelDOTNaNNaND952022-06-03 03:08:2150.009.00111111111111111111111
2022-06-06 00:00:00EZPassTransponder777777777111111111115363NJTP12022-06-03 03:27:3822022-06-03 03:39:5650.004.60111111111111111111111
2022-06-06 00:00:00ELITETransponder777777777111111111115362KTAWichita: I-135 I-235 47th St2022-06-04 15:21:21Southern Entrance2022-06-04 15:47:5750.005.05111111111111111111111
2022-06-06 00:00:00ELITETransponder777777777111111111115357OTAWR-BGCBN2022-06-05 02:05:25WR-STLINE2022-06-05 02:38:1750.009.20111111111111111111111
2022-06-06 00:00:00ELITETransponder777777777111111111115355OTAHEB-NWCLML2022-06-03 07:10:36HEB-NWCLML2022-06-03 07:10:3650.003.95111111111111111111111

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2025-02-12 15:26:21

都不需要通过像行[1]行[2] ...代码>行[15] , nor a loop

直接使用 执行人 (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();

Neither need to specify each column individually by like row[1],row[2]...row[15], nor a loop

but use directly executemany (which's also more performant for DML operations than execute ) such as

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文