My Json data contains the following (but larger)
{
"realms": [
{
"status": true,
"battlegroup": "Shadowburn",
"name": "Zuluhed",
"locale": "en_US",
"queue": false,
"connected_realms": [
"ursin",
"andorhal",
"scilla",
"zuluhed"
],
"timezone": "America/New_York",
"type": "pvp",
"slug": "zuluhed",
"population": "medium"
}
]
}
and this is my code snipet should put the data into the db file ( json data was loaded into data variable(data = json.loads(response)) )
db=sqlite3.connect("temp.db")
c=db.cursor()
for record in data['realms']:
c.execute('INSERT INTO realms (status, name, queue, timezone, type, population) VALUES (?,?,?,?,?,?)', (record['status'], record['name'],record['queue'], record['timezone'],record['type'], record['population']))
Running the script runs without error but checkin the contents of the table there is nothing
# sqlite3 temp.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT * FROM "realms";
sqlite>
sqlite> .tables
realms
sqlite>
I'm new to json and sqlite so I assume im doing something wrong. Thanks
db.commit()afterwards? The cursor won't update the table until you commit the changes.create table realms, and that table doesn't appear in thetemp.dbthat you examine. It is possible that thetemp.dbthat your Python writes to is in a different directory than thetemp.dbthat you examine by hand. Can youprint(os.getcwd())in your Python program?