-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b285600
commit 77fe6cb
Showing
4 changed files
with
2,948 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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_homework.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 REPLACE INTO Member | ||
(user_id, course_id, role) VALUES ( ?, ?, ? )''', | ||
( user_id, course_id, role ) ) | ||
|
||
conn.commit() |
Oops, something went wrong.