Reputation: 41
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()
Upvotes: 4
Views: 5566
Reputation: 49
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).
Upvotes: 2
Reputation: 16411
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
Upvotes: 0