我编写了一个 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)}")
Pyodbc 包含在部署到 Azure Functions 的requirements.txt 文件中。
如果有人能提供帮助那就太好了。
我相信这可能与 Azure 函数没有正确的 ODBC 库有关,但我读到它是预先安装的,所以这应该不是问题。