database: json format and sqlite - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: database: json format and sqlite (/thread-35909.html) |
database: json format and sqlite - izan - Dec-28-2021 Hi! I have to do the following task: The application will read roster data in JSON format, parse the file, and then produce an SQLite database that contains a User, Course, and Member table and populate the tables from the data file. You can base your solution on this code: http://www.py4e.com/code3/roster/roster.py - this code is incomplete as you need to modify the program to store the role column in the Member table to complete the assignment. My programme: import json import sqlite3 conn = sqlite3.connect('rosterdb.sqlite') cur = conn.cursor() # Do some setup cur.executescript(''' DROP TABLE IF EXISTS User; DROP TABLE IF EXISTS Member; DROP TABLE IF EXISTS Course; CREATE TABLE User ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT UNIQUE ); CREATE TABLE Course ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, title TEXT UNIQUE ); CREATE TABLE Member ( user_id INTEGER, course_id INTEGER, role INTEGER, PRIMARY KEY (user_id, course_id) ) ''') fname = input('Enter file name: ') if len(fname) < 1: fname = 'roster_data.json' # [ # [ "Charley", "si110", 1 ], # [ "Mea", "si110", 0 ], str_data = open(fname).read() json_data = json.loads(str_data) for entry in json_data: name = entry[0] title = entry[1] role= entry[2] print((name, title, role)) cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''', ( name, ) ) cur.execute('SELECT id FROM User WHERE name = ? ', (name, )) user_id = cur.fetchone()[0] cur.execute('''INSERT OR IGNORE INTO Course (title) VALUES ( ? )''', ( title, ) ) cur.execute('SELECT id FROM Course WHERE title = ? ', (title, )) course_id = cur.fetchone()[0] cur.execute('''INSERT OR IGNORE INTO Member (role) VALUES ( ? )''', ( role, ) ) cur.execute('''INSERT OR REPLACE INTO Member (user_id, course_id) VALUES ( ?, ?)''', ( user_id, course_id ) ) conn.commit()The the task says: Once you have made the necessary changes to the program and it has been run successfully reading the above JSON data, run the following SQL command: The output should look as follows: With my programme I get the following (I suppose it is what I have to obtain): The problem comes here:Then the task says: Once that query gives the correct data, run this query: You should get one row with a string that looks like XYZZY53656C696E613333 .I think there is something wrong with this query because it does not work. Any ideas? Thank you in advance RE: database: json format and sqlite - ibreeden - Dec-28-2021 (Dec-28-2021, 01:25 PM)izan Wrote: With my programme I get the following (I suppose it is what I have to obtain): I can't believe you get this result by executing: It says: "LIMIT 2" but you get 6 records. It also says "ORDER BY User.name DESC" but your names appear in random order.Please have a sharp look at how you execute this query. |