Azure 函数:“无法打开 lib 'ODBC Driver 18 for SQL Server':找不到文件 (0) (SQLDriverConnect)”)

问题描述 投票:0回答:1

我能够上传一个azure函数python项目,它接收并返回请求,但由于某种原因我收到以下错误:

我尝试使用 ODBC 18,但没有任何结果。 我尝试使用

pyodbc.drivers()
技巧,但它向我展示了这一点:

Azure Functions 似乎有一个空的驱动程序列表,我开始认为它没有安装任何驱动程序。

有没有办法安装ODBC或其他驱动程序? 或者这个问题还有其他解释吗?

这是我用于创建连接的代码,我使用 PyODBC 以及 SQLAlchemy 和 Azure Functions(使用 Linux 和 python 3.11)。

url = URL.create(
            "mssql+pyodbc",
            username=os.environ.get("DB_USER"),
            password=os.environ.get("DB_PASSWORD"),
            host=os.environ.get("DB_HOST"),
            port=1433,
            database=os.environ.get("DB_NAME"),
            query={
                "driver": "ODBC Driver 18 for SQL Server",
                "TrustServerCertificate": "yes",
                "Encrypt": "yes",
            },
        )

    engine = create_engine(url, echo=echo, connect_args={"timeout": 40})

谢谢你。

我尝试更改为另一个驱动程序版本,我希望可以工作,但它仍然给出相同的错误。

python azure-functions odbc pyodbc
1个回答
0
投票

我已经在我的环境中重现了,以下是我的预期结果:

__init__.py

import os
import logging
import pyodbc
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request Rithwik.')
    server1 = 'servername.database.windows.net'
    db1 = 'rith'
    username1 = 'rithwik'
    pwd1 = 'password'
    constring = f"Driver={{ODBC Driver 17 for SQL Server}};Server={server1};Database={db1};Uid={username1};Pwd={pwd1};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
    with pyodbc.connect(constring) as conn:
        with conn.cursor() as c3:
            c3.execute('SELECT SYSTEM_USER;')
            row = c3.fetchone()
    print("User is ",row)
    return func.HttpResponse(f"SQL Query Result: {row}", status_code=200)

requirements.txt:

enter image description here

Output:

enter image description here

enter image description here

enter image description here

尝试将你的代码集成到我的代码中,你将得到我所得到的输出。您也可以参考我的SO-Thread

© www.soinside.com 2019 - 2024. All rights reserved.