我想做的是将 CSV 插入 SQL Server 中的现有表中,而不删除表中的任何数据,但我不断收到错误。这是完整的错误:
Traceback (most recent call last):
File "C:\Users\sampleUser\Desktop\Python Script\Food Services\Import to SSMS.py", line 55, in <module>
cursor.execute(query, row['Visit Date'], row['Client ID'],
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 24 (""): 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)')
如何解决此错误?
这是我的完整代码:
import pandas as pd
import pyodbc
#File path to CSV
file_path = r'C:\Users\sampleUser\Downloads\someFile.csv'
#Read the CSV file into a DataFrame
df = pd.read_csv(file_path)
#Establish connection parameters
server = 'sampleServer'
database = 'sampleDatabase'
username = 'sampleUser'
password = 'PW1234!@#$'
#Connection string
conn_str = (
f"DRIVER={{ODBC Driver 17 for SQL Server}};"
f"SERVER={server};"
f"DATABASE={database};"
f"UID={username};"
f"PWD={password}"
)
#Connect to SQL Server
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
#Define table name
table_name = 'SomeTable'
#Insert DataFrame records one by one
for index, row in df.iterrows():
query = f"""
INSERT INTO {table_name} (Visit Date, Guest ID, Last Name, First Name,
Date of Birth, Guest Age, Guest Gender, Guest Ethnicity,
Guest Ethnicity 2, Street, Line 2, City, County, State, Zip Code,
Housing Type, Latitude, Longitude, Household ID, Household Size, Primary Language,
Diet Restrictions, Taken At (UTC), Taken By, Company Visited,
Company Visited (Short Name), Plan Name, Individuals Served)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
cursor.execute(query, row['Visit Date'], row['Guest ID'],
row['Last Name'], row['First Name'],
row['Date of Birth'], row['Guest Age'],
row['Guest Gender'], row['Guest Ethnicity'],
row['Guest Ethnicity 2'], row['Street'],
row['Line 2'], row['City'], row['County'],
row['State'], row['Zip Code'], row['Housing Type'],
row['Latitude'], row['Longitude'], row['Household ID'],
row['Household Size'], row['Primary Language'],
row['Diet Restrictions'], row['Taken At (UTC)'],
row['Taken By'], row['Company Visited'],
row['Company Visited(Short Name)'], row['Plan Name'],
row['Individuals Served'])
conn.commit()
cursor.close()
conn.close()
我的最终目标是每月自动执行一次此过程,我将使用将由另一个程序执行的批处理文件来运行此过程。 我已经尝试过这个“解决方案”,但我无法让它正常工作,我还尝试将参数数量减少到 23,因为错误发生在 24(我认为这就是错误所说的),但错误仍然存在。任何帮助将不胜感激。
# Convert specific column to float
columns_to_convert = ['Taken By']
for col in columns_to_convert:
df[col] = pd.to_numeric(df[col], errors='coerce')
df['Recorded By'].fillna(0, inplace=True)
答案就在错误消息中:在某些时候,您试图插入无效数据。
找出数据到底出了什么问题的最快方法是进行一些良好的老式打印调试。
在
cursor.execute
之前插入以下代码片段:
print("Next insertion:")
print(f" Visit Date: '{row['Visit Date']}'")
print(f" Guest ID: '{row['Guest ID']}'")
print(f" Last Name: '{row['Last Name']}'")
print(f" First Name: '{row['First Name']}'")
print(f" Date of Birth: '{row['Date of Birth']}'")
print(f" Guest Age: '{row['Guest Age']}'")
print(f" Guest Gender: '{row['Guest Gender']}'")
print(f" Guest Ethnicity: '{row['Guest Ethnicity']}'")
print(f" Guest Ethnicity 2: '{row['Guest Ethnicity 2']}'")
print(f" Street: '{row['Street']}'")
print(f" Line 2: '{row['Line 2']}'")
print(f" City: '{row['City']}'")
print(f" County: '{row['County']}'")
print(f" State: '{row['State']}'")
print(f" Zip Code: '{row['Zip Code']}'")
print(f" Housing Type: '{row['Housing Type']}'")
print(f" Latitude: '{row['Latitude']}'")
print(f" Longitude: '{row['Longitude']}'")
print(f" Household ID: '{row['Household ID']}'")
print(f" Household Size: '{row['Household Size']}'")
print(f" Primary Language: '{row['Primary Language']}'")
print(f" Diet Restrictions: '{row['Diet Restrictions']}'")
print(f" Taken At (UTC): '{row['Taken At (UTC)']}'")
print(f" Taken By: '{row['Taken By']}'")
print(f" Company Visited: '{row['Company Visited']}'")
print(f" Company Visited(Short Name): '{row['Company Visited(Short Name)']}'")
print(f" Plan Name: '{row['Plan Name']}'")
print(f" Individuals Served: '{row['Individuals Served']}'")
当程序崩溃时,可以查看崩溃前的日志,看看数据出了什么问题。