1 python sql-server azure pyodbc azure-functions
我编写了一个 Python 脚本,用于连接到位于 Azure 环境中托管的虚拟机中的 SQL Server。
我已经能够在虚拟机中本地成功连接并运行查询,但是当我部署到 Azure Functions 时,出现以下错误:
('01000', "[01000] [unixODBC][驱动程序管理器]无法打开 lib 'ODBC Driver 17 for SQL Server': 文件未找到 (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)}")
Run Code Online (Sandbox Code Playgroud)
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 驱动程序。
既然 python 3.11 完全支持 azure 函数,我发现它们安装了“ODBC Driver 18 for SQL Server”,而不是安装在 python 3.10 azure 函数上的版本 17。因此conn_str,在您的 中,您需要替换17为18才能使其在 python 3.11 上工作。
| 归档时间: |
|
| 查看次数: |
2340 次 |
| 最近记录: |