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:
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:
Then the task says: Once that query gives the correct data, run this query:
I think there is something wrong with this query because it does not work. Any ideas? Thank you in advance
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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() |
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:
Output:SELECT User.name,Course.title, Member.role FROM
User JOIN Member JOIN Course
ON User.id = Member.user_id AND Member.course_id = Course.id
ORDER BY User.name DESC, Course.title DESC, Member.role DESC LIMIT 2;
The output should look as follows:Output:Zuriel|si430|0
Zofia|si363|0
With my programme I get the following (I suppose it is what I have to obtain):Output:('Mehraz', 'si310', 0)
('Tobie', 'si310', 0)
('Naila', 'si310', 0)
('Asif', 'si310', 0)
('Derren', 'si310', 0)
('Abdulkadir', 'si310', 0)
The problem comes here:Then the task says: Once that query gives the correct data, run this query:
Output:SELECT 'XYZZY' || hex(User.name || Course.title || Member.role ) AS X FROM
User JOIN Member JOIN Course
ON User.id = Member.user_id AND Member.course_id = Course.id
ORDER BY X LIMIT 1;
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
buran write Dec-28-2021, 03:19 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.