gpt4 book ai didi

python - Azure pyodbc.Error : ('01000' , "[01000][unixODBC][Driver Manager]Can' t 打开 lib 'ODBC Driver 17 for SQL Server':找不到文件 (0) (SQLDriverConnect)

转载 作者:行者123 更新时间:2023-12-02 23:11:10 27 4
gpt4 key购买 nike

我的任务是解决执行 Python 的 Azure 函数返回 500 的原因。我是全新的 Azure,需要一些帮助。据我所知,到目前为止,该函数无法连接到数据库,我不确定为什么。

连接功能:

def get_connection(location):

# change directory
os.getcwd()
location = location
os.chdir(location)

# config.DATABASE_CONFIG['driver'] == 'ODBC Driver 17 for SQL Server'
connection_string = 'DRIVER=' + config.DATABASE_CONFIG['driver'] + \
';PORT=1433' + \
';SERVER=' + config.DATABASE_CONFIG['server'] + \
';PORT=1443' + \
';DATABASE=' + config.DATABASE_CONFIG['database'] + \
';UID=' + config.DATABASE_CONFIG['username'] + \
';PWD=' + config.DATABASE_CONFIG['password']

sql_conn = pyodbc.connect(connection_string, autocommit=True)

return sql_conn

堆栈跟踪:

Executing 'Functions.runMetricsHTTP' (Reason='This function was programmatically called via the host APIs.', Id=21ca7ca1-d48a-4282-bcdb-3ed65fa91ff9)
Information
2021-08-12 16:27:26.227
Python HTTP trigger function processed a request.
Information

Error running the process at 2021-08-12T16:27:26.218518+00:00 Traceback (most recent call last): File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 31, in execute return_dict = main.main() File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/main.py", line 19, in main sql_conn = setup.get_connection(".") File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/setup.py", line 21, in get_connection sql_conn = pyodbc.connect(connection_string, autocommit=True) pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
Error

Result: Failure Exception: Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)") Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 345, in _handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args) File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 19, in entrypoint execute(logging) File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 52, in execute raise e File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 31, in execute return_dict = main.main() File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/main.py", line 19, in main sql_conn = setup.get_connection(".") File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/setup.py", line 21, in get_connection sql_conn = pyodbc.connect(connection_string, autocommit=True)
Error

Executed 'Functions.runMetricsHTTP' (Failed, Id=21ca7ca1-d48a-4282-bcdb-3ed65fa91ff9, Duration=42ms)
Error

Result: Failure Exception: Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)") Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 345, in _handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args) File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 19, in entrypoint execute(logging) File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 52, in execute raise e File "/home/site/wwwroot/runMetricsHTTP/__init__.py", line 31, in execute return_dict = main.main() File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/main.py", line 19, in main sql_conn = setup.get_connection(".") File "/usr/local/lib/python3.7/site-packages/gp_portfolio_metrics-0.0.1-py3.7.egg/gp_portfolio_metrics/metrics/setup.py", line 21, in get_connection sql_conn = pyodbc.connect(connection_string, autocommit=True)

最佳答案

您能否尝试将连接字符串中的驱动程序部分更改为以下内容:connection_string = 'DRIVER={适用于 SQL Server 的 ODBC 驱动程序 17};' +\

另请参阅这篇文章: Linux Open Suse "pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")"

最佳实践不是在代码中创建连接字符串,而是在应用程序设置和本地开发的本地设置中设置它。 https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal

您可以像这样设置连接字符串connection_string = os.environ["connectionstringname"](其中connectionstringname是在应用程序本地设置中定义的连接字符串的名称)

关于python - Azure pyodbc.Error : ('01000' , "[01000][unixODBC][Driver Manager]Can' t 打开 lib 'ODBC Driver 17 for SQL Server':找不到文件 (0) (SQLDriverConnect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68762299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com