3

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

Usman Rafiq
  • 137
  • 1
  • 11

1 Answers1

1

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

vindevoy
  • 19
  • 2