#!/usr/bin/env python3
# Print code/title of all subjects
# offered in a given term

import sys
import psycopg2

if  len(sys.argv) < 2:
  print(f"Usage: {sys.argv[0]} Term")
  exit()

try:
  db = psycopg2.connect("dbname=uni")
  cur = db.cursor()
  cur.execute("select id from Terms where code=%s",[sys.argv[1]])
  tup = cur.fetchone()
  q = """
      select s.code, s.name
      from Subjects s join Courses c on s.id = c.subject
      where term = %s order by code
  """
  cur.execute(q,tup)
  for tup in cur.fetchall():
    print(tup[0],tup[1])
except:
   print("DB error")
finally:
   if db:
     db.close()
