4

I have an Azure function witch python that connect a database azure SQL, I’m using the package pyodbc to connect database. On my computer it's working, but when I deploy via vscode, azure does all the python installations based on the requirements and he tries to do the download through website https://github.com/pypa/pip/issues/8368./n , which is out There is another way to access Azure SQL with Python in an Azure Function?? I’m using the above tools Developer in VSCODE in Windows 10 Azure Functions: Linux Type azure function is “HTTP” Local is East 2

My code

Import pyodbc
password = *********
server = 'prd-xx-xxx-xxx-01.database.windows.net'
database = 'prd-xx-xxx-xxx-db-02'
username = 'adm'
driver= '{ODBC Driver 17 for SQL Server}'
conn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
df = pd.read_sql(select_cidades,conn)
conn.close()

2 Answers 2

2

The Python worker comes with an ODBC 17 driver. It's not documented at this time, but you can use it by adding pyodbc to your requirements.txt file in VS Code. See: https://github.com/MicrosoftDocs/azure-docs/issues/54423

The documented way to solve this is to use an environment where you can provide the package. You can create a Docker custom image with the Azure Function runtime and publish in the Azure Cloud (or in an on-premise infrastructure).

https://medium.com/globant/serverless-applications-with-azure-functions-python-and-docker-b594fb90fd4f

Sign up to request clarification or add additional context in comments.

1 Comment

It seems current versions of Python worker do not have ODBC drivers anymore. Maybe Azure guys chose to remove it at some point.
0

You also could use pymssql to connect to the Azure SQL database.

Here's the example code:

import pymssql  
    conn = pymssql.connect(server='yourserver.database.windows.net', user='yourusername@yourserver', password='yourpassword', database='AdventureWorks')  
    cursor = conn.cursor()  
    cursor.execute('SELECT c.CustomerID, c.CompanyName,COUNT(soh.SalesOrderID) AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;')  
    row = cursor.fetchone()  
    while row:  
        print str(row[0]) + " " + str(row[1]) + " " + str(row[2])     
        row = cursor.fetchone()

Please ref: concept connecting to SQL using pymssql

5 Comments

Unfortunately it didn't work, I used this "pymssql " in previous versions of SQL, but in this project my version of SQL is Microsoft SQL Azure (RTM) - 12.0.2000.8 Sep 11 2020 22:32:15 Copyright (C) 2019 Microsoft Corporation. Microsoft documentation ("pymssql") is for python2 yet. Any other tips? @Leon Yue
pyodbc is the way to go, and it looks like you are doing everything right. Can you please post the error, from your post is not entirely clear what's happening.
This is mensagen when i try to deploy:
@ mauridb, This is mensagen when i try to deploy in vscode: n ERROR: Failed building wheel for pyodbc\nDEPRECATION: Could not build wheels for pyodbc which do not use PEP 517. pip will fall back to legacy 'setup.py install' for these. pip 21.0 will remove support for this functionality. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at github.com/pypa/pip/issues/8368.\n
Did you use pip to install pyodbc to your virtual environment? Can you please share requirements.txt entry for pyodbc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.