Home > Software design >  Azure Functions Python connect to Azure SQL DB
Azure Functions Python connect to Azure SQL DB

Time:04-27

I created an Azure Function with Python and want to write some data into an Azure SQL DB.
If I run the code on my local machine via AZ Function Debugger, everything is working. But when I deploy everything to Azure, I only get a message that there is an error (no additional specific information).
I think this is related to the ODBC Driver? I'm using the following code to connect and insert data:

with pyodbc.connect('DRIVER=' driver ';SERVER=tcp:' server ';PORT=' port ';DATABASE=' database ';UID=' username ';PWD='  password   ";Authentication=ActiveDirectoryPassword", timeout=120) as conn:
        with conn.cursor() as cursor:
            try:
                cursor.execute(data)
            except:
                logging.error("Can't execute SQL Query!")             

I use driver= '{ODBC Driver 17 for SQL Server}' as driver.
I assume that this is missing in Azure? How can this issue be fixed? What is the right approach to connect from Azure Functions to an Azure SQL DB via Python?

CodePudding user response:

It seems the ODBC driver is included, it was just poorly documnented:

https://github.com/MicrosoftDocs/azure-docs/issues/54423

There is an example project here:

https://github.com/kevin808/azure-function-pyodbc-MI

The full tutorial including creating the system assigned identity can be found here:

https://techcommunity.microsoft.com/t5/apps-on-azure-blog/how-to-connect-azure-sql-database-from-python-function-app-using/ba-p/3035595

There is currently a SQL Extension under development but it only supports C# at the moment. Python has been requested as an ehancement so you could add your

  • Related