pythonazurepyodbc

Cannot connect to Azure SQL from Azure functions


I am trying to send some data from an Azure function to an Azure SQL DB, it seems that the function's underlying linux image does not have PYODBC driver 18. I am using a managed identity to connect and it worked fine earlier on a linux function.

conn_string = f"Driver={{ODBC Driver 18 for SQL Server}};SERVER=sql-to-dev-.database.windows.net;DATABASE=poc"
database_conn = pyodbc.connect(conn_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})            
database_cursor = database_conn.cursor()

Error

pyodbc.Error: Can't open lib 'ODBC Driver 18 for SQL Server' :azure functions


Solution

  • Connected to the function App linux using KUDU ssh. Printed the contents of /etc/odbcinst.ini and it showed that it had ODBC driver 18.

    I changed 17 to 18 in the conn_string var and it worked.