#!/usr/bin/python # there is this module for obtaining cgi form data #import cgi import sys # define a function to print error messages def error(msg): print """ Error: %s""" % msg print "\n" sys.exit() import psycopg2 #connect to a database try: con = psycopg2.connect(database='timetable') except: error("Error connecting to server") # make a cursor object for this connection # you can make as many as you like curs = con.cursor() # do a query curs.execute("SELECT * FROM profs order by pname") # get the resulting table, a list of lists all = curs.fetchall() #print all #see, the table is a list of sequences for row in all: rw = [] # need list to be mutable for item in row: #print the list for one row on a line if item==None: # get rid of Nulls item = '' rw.append(item) #print in nice columns, using C style format print "%20s %20s %5s %12s %s" % tuple(rw) # and immutable seq. to print curs.close() con.close()