#!/usr/bin/env python3
# Print code and title of all subjects
# matching a partial code 

import sys
import psycopg2

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

# alternative query:
# select code,name from Subjects
# where code like '%%'||%s||'%%' order by code
