Home > Enterprise >  Result: Failure Exception: AttributeError: 'Engine' object has no attribute 'execute&
Result: Failure Exception: AttributeError: 'Engine' object has no attribute 'execute&

Time:01-31

I am quite new to Azure Functions. This code for some reason runs locally but when deployed to an Azure Function, give this error.

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")

Result: Failure Exception: AttributeError: 'Engine' object has no attribute 'execute' Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 452, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*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('''

Can someone explain the reason for this and a possible fix?

Thanks!

CodePudding user response:

The deprecated execute method of engine has been removed in SQLAlchemy 2.0. You need to ensure that you are running the same version of SQLAlchemy in both environments and consider migrating to v2.0.

  • Related