I am trying to insert Pandas Dataframe to SQL Table using PYODBC Package. But when I run the code it says
Error: ('HY004', '[HY004] [Microsoft][ODBC SQL Server Driver]Invalid SQL data type (0) (SQLBindParameter)')
The column types are ok.
Following are the details
Code
for index, row in final_df.iterrows():
print(row)
cursor.execute("INSERT INTO CourseOutline ([ActivityTypeID] ,[ActivityID],[Sequence],[CourseTypeID] ,[Duration], [MinistryCode] ,[FGCCode] ,[Percentage],[ModuleID] ,[PageNo]) values(?,?,?,?,?,?,?,?,?,?)", final_df.ActivityType, final_df.DBID,final_df.Seq,final_df.CourseType,1,'MinistryCodes','FGCCodes',final_df.Percentage,final_df.Module,final_df.PgNo)
cnxn.commit()
cursor.close()
dataframe column types

SQL Column types


I faced the same error and for me the answer in this question worked:
Inserting rows into Microsoft SQL Server using pandas raises precision error
I changed my driver from "SQL Server" to "ODBC Driver 17 for SQL Server"
Also, originally, this worked for me in my Python environment with Pandas 1.x, but it stopped working when I switched to Pandas 2.0. I had to change the driver to make it work in both versions of Pandas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With