Skip to content

Commit 822586f

Browse files
committed
Week 4 Assignment- Many Students in Many Courses
1 parent be67cc1 commit 822586f

File tree

3 files changed

+1931
-0
lines changed

3 files changed

+1931
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import json
2+
import sqlite3
3+
4+
conn = sqlite3.connect('assgn.sqlite')
5+
cur = conn.cursor()
6+
7+
# Do some setup
8+
cur.executescript('''
9+
DROP TABLE IF EXISTS User;
10+
DROP TABLE IF EXISTS Member;
11+
DROP TABLE IF EXISTS Course;
12+
13+
CREATE TABLE User (
14+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
15+
name TEXT UNIQUE
16+
);
17+
18+
CREATE TABLE Course (
19+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
20+
title TEXT UNIQUE
21+
);
22+
23+
CREATE TABLE Member (
24+
user_id INTEGER,
25+
course_id INTEGER,
26+
role INTEGER,
27+
PRIMARY KEY (user_id, course_id)
28+
)
29+
''')
30+
31+
fname = input('Enter file name: ')
32+
if len(fname) < 1:
33+
fname = 'roster_data.json'
34+
35+
# [
36+
# [ "Charley", "si110", 1 ],
37+
# [ "Mea", "si110", 0 ],
38+
39+
str_data = open(fname).read()
40+
json_data = json.loads(str_data)
41+
42+
for entry in json_data:
43+
44+
name = entry[0];
45+
title = entry[1];
46+
role = entry[2];
47+
48+
print((name, title, role))
49+
50+
cur.execute('''INSERT OR IGNORE INTO User (name)
51+
VALUES ( ? )''', ( name, ) )
52+
cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
53+
user_id = cur.fetchone()[0]
54+
55+
cur.execute('''INSERT OR IGNORE INTO Course (title)
56+
VALUES ( ? )''', ( title, ) )
57+
cur.execute('SELECT id FROM Course WHERE title = ? ', (title, ))
58+
course_id = cur.fetchone()[0]
59+
60+
cur.execute('''INSERT OR REPLACE INTO Member
61+
(user_id, course_id, role) VALUES ( ?, ?, ? )''',
62+
( user_id, course_id, role ) )
63+
64+
conn.commit()
Binary file not shown.

0 commit comments

Comments
 (0)