I am trying to save my Json data that I have pulled from a URL, but I am struggling to save it in an SQLite database.
Json:
{"Address":"ff00000a70d57128","Celsius":26.8125,"Fahrenheit":80.2625}
I have looked around and gained from inspiration from:
saving json data into sqlite Convert JSON to SQLite in Python - How to map json keys to database columns properly?
import threading
import json
import urllib.request
import sqlite3
def tempRequest():
# download raw json object
url = "http://10.0.0.111:8080/getdevice?device=type28_1"
data = urllib.request.urlopen(url).read().decode()
# parse json object
obj = json.loads(data)
print(obj)
tempRequest()
conn = sqlite3.connect('test.db')
c = conn.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS data(Celcius TEXT)")
def data_entry():
c.executemany('INSERT INTO data (Celcius) ''VALUES (:Celcius)
conn.commit()
c.close()
conn.close()
create_table()
data_entry()
What i get when i am running this is:
TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not ellipsis
I have tried to change:
obj = json.loads(data)
to
obj = json.loads(str(data))
But i think that the issue is on how i handle the DB part, any advice would be much appreciated. Thanks!
pdbis your friend. Placeimport pdb; pdb.set_trace()anywhere you want to stop, then run the script. The pdb interpreter came up, and you can inspect variable such asdatajust like python interpreter.