Home > Software engineering >  Getting 'bool object not callable' error when inserting data into sql server using pandas
Getting 'bool object not callable' error when inserting data into sql server using pandas

Time:03-15

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?

CodePudding user response:

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