python - Azure函数: Can't open lib 'ODBC Driver 17 for SQL Server'

标签 python sql-server azure azure-functions pyodbc

我编写了一个 Python 脚本,用于连接到托管在 Azure 环境中的虚拟机中的 SQL Server。

我已经能够在虚拟机中本地成功连接并运行查询,但是当我部署到 Azure Functions 时,出现以下错误:

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

几天前我成功运行了脚本并连接到数据库,但由于某种原因,它停止工作,现在出现此错误。

import pyodbc


DatabaseServer = 'Server'
DatabaseName = 'databasename'
conn_str = "Driver={ODBC Driver 17 for SQL Server };Server="+str(DatabaseServer)+';Database='+str(DatabaseName)+";'Trusted_Connection=yes;"

try:
    # Connect to the SQL Server
    conn = pyodbc.connect(conn_str)
    cursor = conn.cursor()

    # Execute the query
    cursor.execute("SELECT TOP 10 hmy FROM Table")

    # Fetch and print the results
    rows = cursor.fetchall()
    results_str = ""
    for row in rows:
        results_str += str(row) + "\n"

    # Close the cursor and connection
    cursor.close()
    conn.close()
    print("Connection to SQL Server Succesful")



except pyodbc.Error as e:
    print(f"Error connecting to SQL Server {str(e)}")

Pyodbc 包含在部署到 Azure Functions 的requirements.txt 文件中。

如果有人可以提供帮助,那就太好了。

我相信这可能与 Azure 函数没有正确的 ODBC 库有关,但我读到它是预安装的,所以这应该不是问题。

最佳答案

当我使用 azure 函数核心工具部署 python 3.11 azure 函数时,发生了这种情况:func azure functionapp publish <name_of_azure_function> 。当我切换回 python 3.10 时,安装了 ODBC Driver 17 for SQL Server 驱动程序。 Azure 函数中对 Python 3.11 的支持处于公共(public)预览版中:https://azure.microsoft.com/en-us/updates/public-preview-support-for-python-311-in-azure-functions/

关于python - Azure函数: Can't open lib 'ODBC Driver 17 for SQL Server' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76764452/

相关文章:

python - 如何在Python中创建N个指数的函数来拟合数据

SQL服务器: Check if variable is null and then assign statement for Where Clause

asp.net - 如何设计 "Country"、 "Province"和 "City"表?

azure - 屏蔽字段格式下缺少社会安全号码格式

mysql - 在 Azure VM 中重新启动后 Docker 容器速度变慢

python - 使用 Tensorflow 2 的 Keras 功能 API 时传递 `training=true`

python - 为斯坦福 NLP 的 NLTK 中的 docker 设置 JAVA_HOME

Python - 读取没有 Pandas 的 Parquet 文件

asp.net - 当我尝试从数据库中删除行时,出现参数过多错误

c# - 将项目部署到 Azure Web 服务时,ASP.NET Core 2.0 Web 应用程序失败并出现 SQLite 错误,但在本地环境中运行良好