我正在嘗試將.csv檔案的內容插入 SQL Server 資料庫。
這是我的代碼:
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()
我在我的 lambda 函式中運行上面的代碼。不知道為什么我會收到這個錯誤;
"errorMessage": "嘗試使用關閉的游標。"
“errorType”:“編程錯誤”"stackTrace":
"檔案 "/var/task/lambda_function.py",第 56 行,在 lambda_handler\n cursor.execute("INSERT INTO HumanResources.DepartmentTest (DepartmentID,Name,GroupName) values(?,?,?)" , row.DepartmentID, row.Name, row.GroupName)
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
你cursor.close()應該在 for 回圈之外:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/427861.html
