【问题标题】:The supplied value is not a valid instance of float. But I'm not asking for a float. Where have I gone wrong?提供的值不是有效的浮点数实例。但我不是要浮动。我哪里出错了?
【发布时间】: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) 个附加参数(handleparamsstmt)。
  • @GordThompson 感谢 Gord,这很有意义。我认为它可能是这样的,但没有找到任何东西。

标签: python sql sql-server pandas pyodbc


【解决方案1】:

【讨论】:

  • 我无法告诉你为什么我在 onw 搜索中没有看到这篇文章,但它似乎成功了。相反,我用“”替换了每个字段,并且似乎工作正常,找到的假定空字符串实际上是 NaN,因此它不适合任何类别。这使我可以在任何实际上是字符串的列中保留零。谢谢教授!
  • 前段时间我遇到了一个非常相似的问题,fillna(0) 也为我解决了问题。很高兴我能帮上忙!
  • 我在设置为允许 NULLS 的 nvarchar() 列上遇到了这个问题。在该列中有np.nan 会产生上面看到的神秘的不存在参数错误。用一些文本替换该列中的NULL/np.nan 值消除了该错误。在使用 El Profesor 的解决方案之前,我通过为产生上述错误的行编写 INSERT INTO SQL 语句(使用 SQL 方言)来验证我的数据格式是否正确——它工作正常,即使是“问题值”是NULL 而不是一些文字。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多