当我尝试将excel文件数据插入sql server
** pyodbc.Error :(“ HY010”,“ [HY010] [Microsoft] [SQL Server的ODBC驱动程序17]功能序列错误(0)(SQLParamData)”)**
import pandas as pd
import pyodbc
import numpy as np#
import time
# start_time = time.time()
df = pd.read_excel("C:\inetpub\wwwroot\File_Upload\PythonScripts\TestPayBook.xlsx")
values = {
'Exit Date': '1900-01-01',
'Date of Joining': '9999-01-01',
'DOB': '1900-01-01'
}
df = df.fillna(value = values)
df1 = df.replace(np.nan, '', regex = True)
df_melt = df1.melt(id_vars = ['Pay Year', 'Pay Month', 'Employee ID', 'NAME', 'COUNTRY', 'CATEGORY', 'Grade', 'Band', 'Expat/Local',
'GATP/Non GATP', 'Designation', 'LOCATION / ENTITY', 'SEX',
'DOB', 'Age', 'Date of Joining', 'TENURE', 'PRODUCT', 'Cost Centre', 'Exit Status',
'Exit Date', 'Exit Reason', 'Currency', 'Department'
])
paymonthlist = df_melt.get("Pay Month").unique().tolist()
payyearlist = df_melt.get("Pay Year").unique().tolist()
paycountrylist = df_melt.get("COUNTRY").unique().tolist()
paylocationlist = df_melt.get("LOCATION / ENTITY").unique().tolist()
payexpatlist = df_melt.get("Expat/Local").unique().tolist()
# print("transpose done")# print("--- %s seconds ---" % (time.time() - start_time))
server = 'xxxxxxx'
database = 'xxxxxxx'
username = 'xxxxxxx'
password = 'xxxxxxx@123'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password + ';TDS_Version=7.2;autocommit=False')
cursor = cnxn.cursor()
cursor.fast_executemany = True# Delete operation
DeleteQuery = 'Delete from TB_PAYROLL_TRANS where [Pay Month] in (' + ','.join(('\'' + x + '\'') for x in paymonthlist) + ') and [Pay Year] in (' + ','.join(str(x) for x in payyearlist) + ') and Country in (' + ','.join(('\'' + x + '\'') for x in paycountrylist) + ') and [LOCATION / ENTITY] in (' + ','.join(('\'' + x + '\'') for x in paylocationlist) + ') and [Expat/Local] in (' + ','.join(('\'' + x + '\'') for x in payexpatlist) + ')'
cursor.execute(DeleteQuery)
# print("DeleteQuery done")# print("--- %s seconds ---" % (time.time() - start_time))
params = [tuple(x) for x in df_melt.values]
BulkInsertQuery = ''
'Insert into TB_PAYROLL_TRANS_JUNC ([Pay Year], [Pay Month], [Employee ID], NAME, COUNTRY, CATEGORY, Grade, Band, [Expat/Local], [GATP/Non GATP] , Designation, [LOCATION / ENTITY], SEX, DOB, Age, [Date of Joining], TENURE, PRODUCT, [Cost Centre], [Exit Status], [Exit Date], [Exit Reason], Currency, Department, variable, value)
values( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? )
''
'
cursor.executemany(BulkInsertQuery, params)
# print("Insert done")# print("--- %s seconds ---" % (time.time() - start_time))
cursor.execute("EXEC [TB_DATA_REPO].P_Payroll_Trans_Junction")
cnxn.commit()
# print("--- %s seconds ---" % (time.time() - start_time))