-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJASON_DB_COMPOSITE.py
More file actions
72 lines (55 loc) · 1.97 KB
/
JASON_DB_COMPOSITE.py
File metadata and controls
72 lines (55 loc) · 1.97 KB
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
71
72
import json
import sqlite3
conn = sqlite3.connect('members_db.db')
cur = conn.cursor()
cur.executescript('''
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS members;
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS courses (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS members (
user_id INTEGER NOT NULL,
course_id INTEGER NOT NULL,
role INTEGER NOT NULL,
PRIMARY KEY (user_id, course_id)
)''')
json_fp = open("roster_data.json") #open the file and create a file pointer
obj = json.load(json_fp) #load the JSON to an object
for entry in obj:
name = entry[0]
course = entry[1]
role = entry[2]
#print "Name is: " + name
#print "Course is: " + course
cur.execute("INSERT OR IGNORE INTO users (name) VALUES ( ? )", (name,))
user_id = cur.execute("SELECT id FROM users WHERE name = ?", (name,)).fetchone()[0]
#print "user_id is:" + str(user_id)
cur.execute("INSERT OR IGNORE INTO courses (name) VALUES ( ? )", (course,))
course_id = cur.execute("SELECT id FROM courses WHERE name = ?", (course,)).fetchone()[0]
#print "course_id is:" + str(course_id)
cur.execute("INSERT OR IGNORE INTO members (user_id, course_id, role) VALUES (? , ? , ?)", (user_id, course_id, role))
print "*************\n Now we are constructing....\n"
results = cur.execute('''
SELECT users.name AS Username, courses.name AS Title, members.role AS Role
FROM members
JOIN users ON users.id=members.user_id
JOIN courses ON members.course_id=courses.id
ORDER BY Username
''')
entry = results.fetchone()
while entry:
print "******"
print entry[0]
print entry[1]
print entry[2]
print "\n"
entry = results.fetchone()
conn.commit()
conn.close()