#!/usr/bin/python import sys # define a function to print error messages def error(msg): print """ Error: %s""" % msg print "\n" sys.exit() # script to show the test table in jdatabase jensen print "Content-type: text/html\n" print """

Search results

This is what matches""" import cgi import cgi #helpful module form = cgi.FieldStorage() #form is now an object with the form data name = form.getvalue("name") #a string if name==None: error("You must enter a name") print name name = name.replace("'","''") #GUARD against stray ' for use in SQL statements import pg #module for postgresql database con=pg.connect("jensen") try: result=con.query("select * from test where name ilike '%"+name+"%'") except pg.DatabaseError, message: #one error class, 'message' is #the error string print "Exception raised..." error( message) #informative error message rows = result.getresult() print '' for row in rows: print ' ' #one table row per database row for item in row: print ' ' print ' ' print '
',item,'
' print ''