Error when trying to use pyobdc to connect a SQL Server in a Flask-Python Application on Azure App Service

Question:

I am developing a web application using Python and Flask.

My application accesses a Microsoft SQL Server using pyobdc. It runs locally.

When I deployed it to Azure App Service and used the application it would crash when it needed to access the server, giving me this error:

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")

Random Details:

  • Azure Python Version: 3.8.12

  • Linux Details: Debian GNU/Linux 9 (stretch)

I tried using the kudu bash terminal in my Azure App Service to manually install it using the instructions in this ODBC driver linux link. But when I try, it tells me that sudo is not a command. (Is this an issue regarding Azure limitations or IT Permissions?)

I have also tried upgrading to ODBC Driver 18 for SQL Server to no avail.

I have read in some places that Microsoft Azure App Services does not support ODBC nor the installation of it.

Is this true? Is there a way around this issue?

And are there any alternative methods connecting the MS SQL Server to a python flask web app deployed on azure?

I have also checked the odbc.ini and odbcinst.ini files in /etc, and found that they were empty.

Asked By: Kiet Le

||

Answers:

Okay I solved my issue.

I installed the ODBC Driver 17 for SQL Server via the dockerfile. I used the instructions on how to install found here using the ODBC 17 Debian version. But it didn’t fully work because of some error so I modified slightly.

This is what I added to my docker:

#What I added to deal with the errors I was getting:
RUN apt-get update -y
RUN apt install unixodbc -y

#Installation instructions from microsoft link:
RUN su
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools

#Checking the installation
RUN cat /etc/odbcinst.ini
        
#Create the odbc.ini file
RUN echo -e '[voip]nDescription = whatevernDriver      = ODBC Driver 17 for SQL ServernServer      = whatevernUser        = whatevernPassword    = whatevernPort        = whatevernDatabase    = whatever' > odbc.ini

#Moving odbc.ini to the etc directory
RUN mv odbc.ini etc

#Checking contents of odbc.ini
RUN cat /etc/odbc.ini 

I had to edit the odbc.ini myself because it was empty. But the odbcinst.ini should be populated after running the commands provided by the microsoft instructions.

etc/odbcinst.ini should have something like this:

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.1.1
UsageCount=1

etc/odbc.ini should look something like this:

[voip]
Description = whatever
Driver      = ODBC Driver 17 for SQL Server
Server      = whatever
User        = whatever
Password    = whatever
Port        = whatever
Database    = whatever

You’ll have to edit the odbc.ini yourself if you have this issue. But what I did was create an odbc.ini file. And then moved it into /etc.

Answered By: Kiet Le