-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4.py
More file actions
64 lines (45 loc) · 1.33 KB
/
q4.py
File metadata and controls
64 lines (45 loc) · 1.33 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
import psycopg2
import sys
courseName = sys.argv[1] if len(sys.argv) > 1 else "ENGG"
view1 = '''create or replace view getCourseEnrolments as
select c.course_id, count(*)
from course_enrolments c
group by course_id;'''
query = '''select t.name, s.code, g.count
from subjects s join courses c on (s.id = c.subject_id)
join terms t on (c.term_id = t.id)
join getCourseEnrolments g on (c.id = g.course_id)
join classes cl on (cl.course_id = c.id)
where s.code like '{}%';'''.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(view1)
except Exception as e:
print("Error creating view")
print (e)
try:
cur.execute(query)
except Exception as e:
print("Error selecting from table")
print (e)
termDict = {}
for name,code,count in cur.fetchall():
if name not in termDict:
termDict[name] = {}
codeCounts = termDict[name]
codeCounts[code] = count
else:
codeCounts = termDict[name]
if code not in codeCounts:
codeCounts[code] = count
for term in sorted(termDict):
print("{}".format(term))
codeCounts = termDict[term]
for course in sorted(codeCounts):
print(" {}({})".format(course, codeCounts[course]))
conn.close()