''pyodbc.Cursor' object has no attribute 'fast_executemany'

I have a problem, I have a web app that is using fast_executemany in order to insert into the database. When running it on localhost it works with no problem, but when deploying it to Azure, I get

'pyodbc.Cursor' object has no attribute 'fast_executemany'

I am using

  • Python 2.7
  • AZURE SQL server DB
  • pyodbc==4.0.24

The web app is stored in Azure



Solution 1:[1]

Azure provides us a tutorial about how to use Python to connect to an Azure SQL database and use Transact-SQL statements to query data.

And if we want to use fast_executemany attribute, we must set cursor.fast_executemany = True

We can modify the sample like this:

import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.fast_executemany = True
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
    print (str(row[0]) + " " + str(row[1]))
    row = cursor.fetchone()

I think you can try again. If you still have other issue, please let me know, and I will do my best to help you.

Hope this can helps you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community