Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid SQL data type (0) (SQLBindParameter)')

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 Dataframe column Types

SQL Column types SQL Column Types

First 3 Records of dataframe

like image 485
Usman Rafiq Avatar asked Dec 27 '25 21:12

Usman Rafiq


1 Answers

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

like image 95
vindevoy Avatar answered Dec 31 '25 00:12

vindevoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!