Trying to insert data into database but I keep getting this error:
Traceback (most recent call last):
File "business.py", line 25, in <module>
data = fetch_data(con)
File "business.py", line 18, in fetch_data
result.append((entry['address'], entry['postalcode'],
KeyError: 'address'
Still new to python and coding. Appreciate your help, folks!
What I have so far:
import requests
import sqlite3
URL = 'http://url/url/url/{}.json'
def fetch_data(con):
cur = con.cursor()
cur.execute("SELECT identifier FROM restaurant WHERE res_creditnote = 'No' LIMIT 6")
result = []
for row in cur:
r = requests.get(URL.format(row[0]))
entry = r.json()
result.append((entry['address'], entry['postalcode'],
entry['postal'], entry['statenumber'], entry['state'],
entry['countrycode'], entry['country']))
return result
con = sqlite3.connect("business.db")
data = fetch_data(con)
cur = con.cursor()
cur.executemany('INSERT INTO hotel (address, postalcode, '
'postal, statenumber, state, '
'countrycode, country) VALUES (?, ?, ?, ?, ?, ?, ?);', data)
con.commit()
con.close()
EDIT
I try to fetch json data like this:
"businessaddress":{"address":"Stockholm road 11","postalcode":"045432","postal":"Stockholm","statenumber":"45141","state":"Stockholm","countrycode":"SW","country":"Sweden"}