我对Azure函数很陌生。由于某种原因,此代码在本地运行,但当部署到Azure函数时,会出现此错误。
from sqlalchemy import create_engine, event
import textwrap
import base64
import pandas as pd
import logging
import pyodbc
def sql_run_query(data_base,service_date):
driver = '{ODBC Driver 17 for SQL Server}'
server_name = **********************************
database_name = data_base
server='{server_name},1433'.format(server_name=server_name)
username = **********************************
password = **********************************
connection_string=textwrap.dedent('''Driver={driver};Server={server};Database={database};Uid={username};Pwd={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'''.format(driver=driver,server=server,database=database_name,username=username,password=password))
engine = create_engine('mssql+pyodbc:///?odbc_connect='+connection_string)
#engine =create_sql_engine(data_base)
engine.execute('declare @service_date_var as varchar(100) = '+service_date+'delete from [dbo].[performance] where service_date = @service_date_var')
logging.info(f"query in {data_base} run succesfully")
print(f"query in {data_base} run succesfully")
结果:失败异常:属性错误:"Engine"对象没有属性"execute"堆栈: 文件"/azure-functions-host/worker/python/3.9/LINUX/X64/azure_functions_worker/www.example.com ",第452行,在_handle__invocation_request中调用_result =等待自身。_loop. run_in_executor(文件"/usr/local/lib/python3.9/concurrent/futures/www.example.com",第58行,在运行结果=自身。fndispatcher.py文件"/azure-functions-host/worker/python/3.9/LINUX/X64/azure_functions_worker/www.example.com ",第215行,原始调用 Package 结果=函数thread.py('kad_smc',日期)文件"/home/site/wwwroot/sql_uploader/sql_uploader. py ",第74行,在sql_run_query引擎中。执行('''(*self.args,**self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 718, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/BlobTrigger2/init.py", line 40, in main sql_run_query('kad_smc',dte) File "/home/site/wwwroot/sql_uploader/sql_uploader.py", line 74, in sql_run_query engine.execute('''
有人能解释一下原因和可能的解决办法吗?
谢谢!
1条答案
按热度按时间lmvvr0a81#
SQLAlchemy 2.0中已删除
engine
的过时execute
方法。您需要确保在两种环境中运行相同版本的SQLAlchemy,并考虑将migrating升级到v2.0。