-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroster.py
73 lines (56 loc) · 1.85 KB
/
roster.py
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
73
import json
import sqlite3
conn=sqlite3.connect("try.sqlite")
cur=conn.cursor()
cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Course;
DROP TABLE IF EXISTS Member;
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)
);
''')
filename=input("Enter the file name: ")
if (len(filename)<1): filename="roster_data.json"
data=open(filename).read()
jsdata=json.loads(data)
datax=list()
for piece in jsdata:
name=piece[0]
title=piece[1]
role=piece[2]
print(name, title)
#print('Inserting... ')
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) VALUES (?,?) ''', (user_id, course_id))
# cur.execute('''SELECT user_id FROM Member WHERE course_id=?''', (course_id,))
# member_user_id=cur.fetchone()[0]
#
# cur.execute('''SELECT course_id FROM Member WHERE user_id=?''', (user_id,))
# member_course_id=cur.fetchone()[0]
cur.execute('''UPDATE Member SET role=? WHERE user_id=? AND course_id=?''',(role,user_id,course_id))
cur.executescript('''
SELECT 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
''')
print(cur.fetchone())
conn.commit()
cur.close()