【发布时间】:2021-05-10 03:10:06
【问题描述】:
情况是我通过 API 将表拉入 python 数据框。此数据框被直接拉入 SQL 服务器。 我遇到了一个问题,我遇到了一个空字符串或空单元格,something 期望是浮点数据类型。即使我在 SSMS 中预建了没有单个浮点类型列的表,我仍然收到此错误。
我将相同的数据提取到 CSV 中进行故障排除,我们可以看到第 78 行数据在第 9 列中有一个空单元格。 我的 python 代码成功地将 77 行推送到 SQL 中,然后失败。如果我只是给列指定类型 nvarchar,那么当出现空字符串时它不应该抱怨。
谁能告诉我为什么我在第 12 个参数(当我只有 9 列时)出现错误,并且它希望成为一个浮点数,什么时候它应该满足于作为一个字符串?
这里是一张图片,显示所有部分都聚集在一起,但并不完全聚集在一起。 A lot going on
这里是可能适用或有用的代码。我已经注释掉了所有其他列以进行故障排除,并且没有在此处复制它们。
for i in range(0, 5):
now = datetime.now()
print ('loading stage', i+1, 'of', number_of_locations, 'location:', all_unique_locations[i], ' current time:' , now.strftime ("%H:%M:%S"))
current_stage = stageSummary.get_stage_summary_by_location_text(access_token, all_unique_locations[i], False )
df = convert_string_to_dataframe(current_stage)
all_stage_summary_df = all_stage_summary_df.append(df)
connStr = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER=<redacted>;DATABASE=AAV_DEVELOPMENT;Trusted_Connection=yes')
cursor = connStr.cursor()
for index,row in all_stage_summary_df.iterrows():
print("Adding row ");
cursor.execute("INSERT INTO dbo.StageSummaries([Primary_Location]\
,[JobNo]\
,[Stage]\
,[Top_Depth_m]\
,[Elapsed_Time_hh_mm_ss]\
,[Start_Time]\
,[End_Time]\
,[Pumping_Time_hh_mm_ss]\
,[Mainline_Pressure_Min_MPa]\
)values (?,?,?,?,?,?,?,?,?)"
,row['Primary Location']
,row['JobNo']
,row['Stage #']
,row['Top Depth (m)']
,row['Elapsed Time (hh:mm:ss)']
,row['Start Time']
,row['End Time']
,row['Pumping Time (hh:mm:ss)']
,row['Mainline Pressure Min (MPa)']
)
connStr.commit()
cursor.close()
connStr.close()
我看到几个问题有相同的错误,但他们没有我找到的很多细节或解决方案。
错误
('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 12 (""): 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)'\)
【问题讨论】:
-
“为什么我的第 12 个参数出错(当我只有 9 列时)” - ODBC 驱动程序将 pyodbc
.execute()语句转换为 sp_prepexec 调用和 @ 987654326@ 在绑定参数开始之前采用三 (3) 个附加参数(handle、params和stmt)。 -
@GordThompson 感谢 Gord,这很有意义。我认为它可能是这样的,但没有找到任何东西。
标签: python sql sql-server pandas pyodbc