0

I am new to Azure Functions and am hitting an error I can't seem to get past. The Azure function (Python) is supposed to pull data from an API call and insert the results into an Azure SQL db. I am doing local debugging and keep getting this error. I have verified that all the column names and data types are matching. Any suggestions would be greatly appreciated.

Thanks for any assistance!!!!

import logging
import requests
import pyodbc
import pandas as pd
import azure.functions as func
from datetime import date
# from azure.identity import DefaultAzureCredential

server = 'xxx' 
database = 'xxx' 
username = 'xxx' 
password = 'xxx' 
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()

def get_properties():
    params = {
            'IntegrationPartnerID': 'xxxx',
            'ApiKey': 'xxx',
            'AccountID': 'xxx',
        }
    url = 'https://api.myresman.com/Account/GetProperties'

    data=requests.post(url, params)
    response=data.json()

    df=pd.json_normalize(response, record_path='Properties')
    
    df = df.rename(columns={'PropertyID':'propertyid', 'Name':'property_name', 'StreetAddress':'street_address', \
    'City':'city', 'State':'state_code', 'Zip':'zip_code', 'Phone':'phone', 'Email':'email', \
        'Manager':'manager','CurrentPeriod.Start': 'currentperiod_start', \
            'CurrentPeriod.End': 'currentperiod_end'})
    
    df['propertyid']=df['propertyid'].astype(str)
    df['property_name']=df['property_name'].astype(str)
    df['street_address']=df['street_address'].astype(str)
    df['city']=df['city'].astype(str)
    df['state_code']=df['state_code'].astype(str)
    df['zip_code']=df['zip_code'].astype(str)
    df['phone']=df['phone'].astype(str)
    df['email']=df['email'].astype(str)
    df['manager']=df['manager'].astype(str)
    df['currentperiod_start']=pd.to_datetime(df['currentperiod_start'], format='%Y-%m-%d')
    df['currentperiod_end']=pd.to_datetime(df['currentperiod_end'], format='%Y-%m-%d')
    df['as_of_date']=date.today()
    return df


def main(mytimer: func.TimerRequest) -> None:
    gp_data=get_properties()
    for index, row in gp_data.iterrows():
                cursor.execute("""INSERT INTO dbo.get_properties ('propertyid', 'property_name', 'street_address', 
            'city', 'state_code', 'zip_code', 'phone', 'email', 'manager', 'currentperiod_start', 
                'currentperiod_end') values(?,?,?,?,?,?,?,?,?,?,?,?)""", \
                row.propertyid, row.property_name, row.street_address, row.city, row.state_code, row.zip_code, \
                    row.phone, row.email, row.manager, \
                     row.currentperiod_start, row.currentperiod_end,row.as_of_date)
    cnxn.commit()
    cursor.close()

And here's the error:

[2021-03-22T21:05:02.971Z] System.Private.CoreLib: Exception while executing function: Functions.get-properties-api-call. System.Private.CoreLib: Result: Failure Exception: ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'propertyid'. (207) (SQLExecDirectW); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'property_name'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'street_address'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'city'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'state_code'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'zip_code'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'phone'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'email'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'manager'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'currentperiod_start'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'currentperiod_end'. (207); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)") Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 355, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "C:\Users\marks\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.8/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 542, in _run_sync_func return func(**params) File "C:\Users\marks\upwork_files\Taylor\get_properties\get-properties-api-call_init.py", line 54, in main cursor.execute("""INSERT INTO dbo.get_properties ('propertyid', 'property_name', 'street_address',

4
  • In SQL Server 'propertyid' is a string literal. You probably want to use propertyid in the column list of your INSERT statement. Commented Mar 22, 2021 at 21:23
  • IOW - just list the names of your table in the insert list without string delimiters. E.g., INSERT INTO dbo.get_properties (propertyid, property_name, ... Commented Mar 22, 2021 at 22:41
  • That did it !!!! THANK YOU SOOOOOO MUCH! Commented Mar 22, 2021 at 23:31
  • @bossjimmark Is the error solved now? I just help them post it as answer, you can think about accept it as answer. This can be beneficial to other community members. Thank you. Commented Mar 22, 2021 at 23:44

1 Answer 1

0

Very thanks for AlwaysLearning and SMor's help.

  • In SQL Server 'propertyid' is a string literal. You probably want to use propertyid in the column list of your INSERT statement.
  • IOW - just list the names of your table in the insert list without string delimiters. E.g., INSERT INTO dbo.get_properties (propertyid, property_name, ...

This can be beneficial to other community members.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.