I am trying to insert a .csv
file's contents into a SQL Server database.
Here is my code:
cursor = cnxn.cursor()
cursor.execute("Truncate table HumanResources.DepartmentTest") # Truncate old table contents
# Insert Dataframe into SQL Server:
for index, row in df.iterrows():
cursor.execute("INSERT INTO HumanResources.DepartmentTest (DepartmentID,Name,GroupName) values(?,?,?)", row.DepartmentID, row.Name, row.GroupName)
cnxn.commit()
cursor.close()
I am running above code in my lambda function. Not sure why I am getting this error;
"errorMessage": "Attempt to use a closed cursor."
"errorType": "ProgrammingError""stackTrace":
" File "/var/task/lambda_function.py", line 56, in lambda_handler\n cursor.execute("INSERT INTO HumanResources.DepartmentTest (DepartmentID,Name,GroupName) values(?,?,?)", row.DepartmentID, row.Name, row.GroupName)
Can anyone help me with this issue?
CodePudding user response:
Your cursor.close()
should be outside of the for loop:
for index, row in df.iterrows():
cursor.execute("INSERT INTO HumanResources.DepartmentTest (DepartmentID,Name,GroupName) values(?,?,?)", row.DepartmentID, row.Name, row.GroupName)
cnxn.commit()
cursor.close()