pythonazurepyodbcmsodbcsql17

ODBC Driver 17 not found in deployed back but works in localhost


I hope I am not repeating a post, I haven't found a solution to my problem yet. I am a junior dev and I am currently trying to push in production a new version of an existing website. I work in python in version 3.9.13 and I try to access to the database (on Azure) through Azure functions. When I work on my localhost, everything is fine but when I connect my local front to the deployed dev version of the back, I have this error appearing:

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

As per procedure, it tries 5 times to connect to the database then crashes. The same event appears when I am on the deployed front dev version of the website connected to the deployed back dev version. It crashes an Err 500.

I don't understand why because I already downloaded the correct version of the ODCB on my computer, it works on the running main branch, it is in the requirements.txt document that is loaded during the deployment. I haven't touched it but here is how the connexion to the DB is coded:

def connect_to_database(server, database, driver, DEBUG_MODE):
        logging.info("ENTERING connect_to_database")
        logging.info(f"Driver: {driver}")
        logging.info(f"Database: {database}")
        logging.info(f"Server: {server}")
        conn = None

        retry_flag = True
        retry_count = 0
        connection_string = (
            f"Driver={driver};Server=tcp:{server};PORT=xxxx;Database={database};"
            "Encrypt=yes;TrustServerCertificate=no;Connection Timeout=5;"
        )

        if DEBUG_MODE:
            connection_string += "UID=my_mail@mail.com;Authentication=ActiveDirectoryInteractive"
        else:
            connection_string += "Authentication=ActiveDirectoryMsi"

        while retry_flag and retry_count < 5:
            try:
                logging.info("Attempting to connect to database...")
                pyodbc.pooling = True
                conn = pyodbc.connect(connection_string)
                retry_flag = False
                logging.info("Database connection successful.")
            except Exception as e:
                logging.error(f"Retry {retry_count + 1} failed: {e}")
                retry_count += 1
                time.sleep(1)

        if conn:
            logging.info("DB connection OK")
        else:
            logging.error("Failed to connect to the database after retries")

        return conn

    def connect_to_MPAPPS():
        logging.info("ENTERING connect_to_MPAPPS")
        conn = connect_to_MPAPPS_Conn()
        if conn:
            logging.info(f"conn result: {conn}")
            cursor = conn.cursor()
            logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS")
            return cursor
        else:
            logging.error("Failed to obtain a database connection")
            return None

    def connect_to_MPAPPS_Conn():
        logging.info("ENTERING connect_to_MPAPPS_Conn")
        server = "server address"
        logging.info(f"Server: {server}")
        database = "database_name"
        logging.info(f"Database: {database}")
        driver = "{ODBC Driver 17 for SQL Server}"
        logging.info(f"Driver: {driver}")

        DEBUG_MODE = 1 if os.getenv("AZURE_FUNCTIONS_ENVIRONMENT") == "Development" else 0
        conn = connect_to_database(server, database, driver, DEBUG_MODE)
        logging.info(f"conn: {conn}")
        logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS_Conn")
        return conn

I tried console.logging everything I could to have a better understanding of the issue, I tried to call from Postman, I searched everywhere, I checked all the variables I have access to in Azure, I checked if the SQL Driver was the same as the main (it is), I can see it downloaded on my computer, I haven't foud a different variable between main and dev...

Please don't hesitate to ask any questions if the info I provided is incomplete.


Solution

  • ODBC Driver 17 not found in deployed back but works in localhost

    ODBC Driver 17 is currently (as of now) is not working with azure functions in Portal.

    With ODBC Driver 17 :

    enter image description here

    There is a new update, ODBC Deriver 18 which is working with azure functions in portal:

    f"Driver={{ODBC Driver 18 for SQL Server}};Server={ri_s1};Database={db1};Uid={ri_un};Pwd={r_passwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
    

    Sample Code:

    import logging as rilg
    import azure.functions as func
    import pyodbc
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        rilg.info('Hello Rithwik Bojja, Azure Function is Started')
        ri_un = 'rithwikusrename'
        r_passwd = 'pssword'
        ri_s1 = 'rithwik.database.windows.net'
        db1 = 'dbanme'
        ri_cs = f"Driver={{ODBC Driver 18 for SQL Server}};Server={ri_s1};Database={db1};Uid={ri_un};Pwd={r_passwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
        with pyodbc.connect(ri_cs) as ri_cn:
            with ri_cn.cursor() as ri:
                ri.execute('SELECT SYSTEM_USER;')
                rith = ri.fetchone()
        print("Hello Rithwik Bojja the User is : ",rith)
        rilg.info('Hello Rithwik Bojja, Azure Function is Executed')
        return func.HttpResponse(f"Hello Rithwik Bojja the User is: {rith}", status_code=200)
    

    requirements.txt:

    azure-functions
    pyodbc
    

    Output:

    enter image description here