简体   繁体   中英

Getting 'bool object not callable' error when inserting data into sql server using pandas dataframe and pyodbc's fast_executemany()

I've lots of records to be inserted into sql server. I'm using pyodbc and cursor.fast_executemany() to achieve this. The regular cursor.execute() is inserting records too slowly.

I'm following this article as a reference: https://towardsdatascience.com/how-i-made-inserts-into-sql-server-100x-faster-with-pyodbc-5a0b5afdba5

sample code I'm using:

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()

But I keep getting the below error although I don't have any boolean columns.

'bool' object is not callable

I don't know how to surpass this error and I really need to insert records in bulk and quick into my database table.

Any ideas on why this error occurs and how to solve this?

The second line in the following snippet has to be wrong. You set fast_executemany to True and then try to call it with fast_executemany() .

cursor.fast_executemany = True
cursor.fast_executemany(my_insert_statement,df.values.tolist())

I looked at your guide and you have to replace the second line in the snippet with:

cursor.executemany(my_insert_statement,df.values.tolist())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM