[Tutor] Question on pg and pgdb module again

Ms Soo Chong s4046441 at student.uq.edu.au
Mon Aug 16 15:59:13 CEST 2004


Hi,

Is me again...;p

Actually, my supervisor for my thesis had tried something using pg module
and I think it worked pretty well at this stage. However, I'm the one 
responsible to create a web-based browser that do search from the 
database(moncdata)and display it. So I trying to get the simple stuff working 
first before I move on to the tough part.

I wonder which one to use, so right now I'm trying the pgdb module so that
I can at least I'm able to do a comparison between them and reflect them in
my thesis report.

The following worked well for pg module but not for pgdb module:
import pg
db = pg.connect("moncdata", user=username, passwd=None)

Can anyone give me some good suggestion? 

Thank you again.....


# CGI header lines to tell the browser what to expect.
print "Content-type: text/plain"
# print "Content-length: ", len(resultString)
print ""

# We redirect the standard-error stream to the standard-out stream
# so that we can see the error text in the browser.
import sys
sys.stderr = sys.stdout

# It seems that we need to have an appropriate username that matches
# an entry in the postgresql table of users.
import os
username = os.environ.get('USER')
# print "username: ", username, type(username)
if username == None:
    # Assume that the web server has started this script and has the
    # username 'apache'.  To allow this to access the database, we had
    # to create a postgresql user of that name and have no password.
    # This new user is also able to create tables and new users,
    # otherwise the access seems to be blocked.  (This might be a
    # security problem but only for our database tables.)
    username = 'apache'

# Now, we can get to the database...
import pg
db = pg.connect("moncdata", user=username, passwd=None)
qresult = db.query("select * from shot_descriptions where shot_number = 7399")
listOfResults = qresult.dictresult()
# Make sure that we have a string.
resultString = repr(listOfResults)
print "Raw result obtained from database:"
print resultString

print ""
print "Example of pulling the list of dictionary results apart."
for record in listOfResults:
    print "------- start of record -----------"
    for k in record.keys():
        print "key: ", k, "  value:", record[k]
    print "--------- end of record -----------"
db.close()

Cheers,
Shufen



More information about the Tutor mailing list