简体   繁体   中英

Azure Function (python) insert to Azure SQL keep getting an error

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',

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. Eg, INSERT INTO dbo.get_properties (propertyid, property_name, ...

This can be beneficial to other community members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM