我有很多記錄要插入到 sql server 中。我正在使用 pyodbc 并cursor.fast_executemany()實作這一目標。常規cursor.execute()插入記錄太慢。
我正在關注這篇文章作為參考:https ://towardsdatascience.com/how-i-made-inserts-into-sql-server-100x-faster-with-pyodbc-5a0b5afdba5
我正在使用的示例代碼:
query = "SELECT * FROM dbo.my_table"
df = pd.read_sql(query, conn)
my_insert_statement = f"INSERT INTO myschema.new_table(colA, colB, colC, colD, colE) values(?,?,?,?,?)"
cursor = conn.cursor()
cursor.fast_executemany = True
cursor.fast_executemany(my_insert_statement,df.values.tolist())
conn.commit()
cursor.close()
conn.close()
但是盡管我沒有任何boolean列,但我不斷收到以下錯誤。
'bool' object is not callable
我不知道如何克服這個錯誤,我真的需要批量快速地將記錄插入我的資料庫表中。
關于為什么會發生此錯誤以及如何解決此問題的任何想法?
uj5u.com熱心網友回復:
以下代碼段中的第二行必須是錯誤的。您設定fast_executemany為True,然后嘗試使用fast_executemany().
cursor.fast_executemany = True
cursor.fast_executemany(my_insert_statement,df.values.tolist())
我查看了您的指南,您必須將代碼段中的第二行替換為:
cursor.executemany(my_insert_statement,df.values.tolist())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443652.html
標籤:python-3.x 熊猫 pyodbc
上一篇:無法在python上使用替換方法
下一篇:如何根據動態列值選擇行?
