0

I'm running a flask app on google cloud, but I'm having no luck at all using pyodbc. I initially tried

def googleConnection():
    conn = pyodbc.connect(
            "Driver={SQL Server};"
            "Server=server;"
            "Database=dbname;"
            "UID=username;"
            "PWD=password;"
            )
    return conn 

and I got the following error in gcloud logs:

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

It's complaining that it's missing the necessary drivers. So, I ran a query to return pyodbc.drivers() to see if anything was available

def googleConnection():
    drivers = [item for item in pyodbc.drivers()]
    conn = pyodbc.connect(
            f"Driver={drivers[-1]};"
            "Server=server;"
            "Database=dbname;"
            "UID=username;"
            "PWD=password;"
            )
    return conn

and it just returned an empty list, confirming no drivers, but it imports pyodbc as a package, as per my requirements file.

The DB it's trying to connect to is hosted using GCP SQL and there's no issues with auth, so it really is just missing drivers. But, I can't find any documentation on adding these in. I found this that is using gcloud -beta config but it has since been deprecated.

John Hanley
  • 74,467
  • 6
  • 95
  • 159
Graham
  • 107
  • 1
  • 8

1 Answers1

1

You must install the driver first. The ODBC driver is not pip installable.

Install the Microsoft ODBC driver for SQL Server (Linux)

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • thank you John. It's installed `cloudshell:~ (hrace-359815)$ cat /etc/odbcinst.ini [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` But still no change on the error when running the app – Graham Aug 21 '22 at 23:09
  • @Graham I am not sure that will work in Cloud Shell. Install locally or in a Docker container. Global installations do not persist in Cloud Shell. – John Hanley Aug 21 '22 at 23:19