python novice here.
Trying to pull data from API and insert into SQL Server table (existing) for use in BI tool.
Original result from API is XML with a terribly unfriendly-for-a-novice format.
I have managed to parse this (likely not in the most pythonic way and open to suggestions) to .csv format (3 separate files due to nature of nested XML). Now that I have these in .csv I am trying to write them to my SQL-Server tables, one table per .csv but have hit a snag. I am using the code from this answer and all seems okay other than the leading comma that gets created in the column names section of the query. Anyone help me remove that leading comma?
This is the code as I have written currently:
import json
import requests
import pandas as pd
import csv
from pandas.io.json import json_normalize
from datetime import date, timedelta
url = "https://**myAPI_URL.com/Transaction"
paramHeader = '{"Version": "1.0"'
paramHeader += ', "FromDate":"2020-05-01 00:00"'
paramHeader += ', "ToDate": "2020-05-31 00:00"'
paramHeader += ', "MerchantOrgID": null'
paramHeader += ', "CardholderOrgID": null'
paramHeader += ', "CardNumber": null'
paramHeader += ', "DriverID": null'
paramHeader += ', "VehicleID": null'
paramHeader += ', "BillingGroupID": null'
paramHeader += ', "BillingCycleID": null'
paramHeader += ', "EntryMethodID": null'
paramHeader += ', "CardTypeID": null'
paramHeader += ', "TranTypeID": null'
paramHeader += ', "TaxExemptOnly": null}'
headers = {'APIKey': '**myAPI_KEY**'
, 'content-type': 'application/json'
, 'Accept': 'application/json'
, 'parameters': paramHeader}
response = requests.get(url, data='', headers=headers)
if response.status_code == 200:
r = json.loads(response.content.decode('utf-8'))
cleanData = pd.json_normalize(r)
transactionDetails = pd.json_normalize(data=r, record_path='Details', meta=['ID'])
taxes = pd.json_normalize(data=r, record_path=['Details', 'Taxes'],
meta=['ID'])
cleanData.to_csv('**filePath**/mainTransactions.csv')
transactionDetails.to_csv('**filePath**/transactionsDetails.csv')
taxes.to_csv('**filePath/transactionsTaxes.csv')
import pyodbc
connection = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=**serverIP**;PORT=1433;DATABASE=myDBName;UID=myUserID;PWD=myPWord;')
with open('**filePath**/transactionsDetails.csv', 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into transactionDetails({0}) values ({1})'
query = query.format(','.join(columns), ','.join('?' * (int(len(columns)))))
print(query) #for debug purposes
cursor = connection.cursor()
for data in reader:
cursor.execute(query, data)
cursor.commit()
This code results in the following error:
insert into transactionDetails(,RowNumber,RawProductCode,RawUnitPrice,RawAmount,Quantity,ResolvedProductID,ProductCategoryID,ProductCategory,IsFuel,ProductName,ProductCode,IsTaxProduct,ResolvedUnitPrice,ResolvedAmount,Taxes,ID) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
Traceback (most recent call last):
File "**workingDirectory**/myProject.py", line 85, in <module>
cursor.execute(query, data)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ','. (102) (SQLExecDirectW)")
Process finished with exit code 1
Manually executing the same query (using all 1's as test data) after removing the leading comma results in a successful write to DB.
myTableName> insert into transactionDetails(RowNumber,RawProductCode,RawUnitPrice,RawAmount,Quantity,ResolvedProductID,ProductCategoryID,ProductCategory,IsFuel,ProductName,ProductCode,IsTaxProduct,ResolvedUnitPrice,ResolvedAmount,Taxes,ID) values (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
[2020-05-28 20:08:55] 1 row affected in 188 ms
Thanks!