-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq3.py
More file actions
53 lines (38 loc) · 1.15 KB
/
q3.py
File metadata and controls
53 lines (38 loc) · 1.15 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
import psycopg2
import sys
courseName = sys.argv[1] if len(sys.argv) > 1 else "ENGG"
query = '''select s.code, b.name
from subjects s join courses c on (s.id = c.subject_id)
join terms t on (c.term_id = t.id)
join classes cl on (cl.course_id = c.id)
join meetings m on (cl.id = m.class_id)
join rooms r on (r.id = m.room_id)
join buildings b on (b.id = r.within)
where t.name like '19T2'
and s.code like '{}%'
order by b.name;'''.format(courseName)
try:
conn = psycopg2.connect("dbname='a3'")
except Exception as e:
print("Unable to connect to the database")
print(e)
cur = conn.cursor()
try:
cur.execute(query)
except Exception as e:
print("Error selecting from table")
print (e)
buildingDict = {}
for code,name in cur.fetchall():
if name not in buildingDict:
buildingDict[name] = [code]
else:
courseCodes = buildingDict[name]
if code not in courseCodes:
courseCodes.append(code)
for building in sorted(buildingDict):
print("{}".format(building))
courseCodes = sorted(buildingDict[building])
for i in range(len(courseCodes)):
print(" {}".format(courseCodes[i]))
conn.close()