dbname.py

This Python script defines function dbname to connect to our class database on godaddy.com. It describes the structure of the users table.

Try it: run script


01: #!/usr/bin/python
02: import MySQLdb
03: 
04: def dbopen():
05:     'open ece538 database on godaddy.com'
06:     try:
07:         db = MySQLdb.connect(
08:             host="hosturl",
09:             user="username",
10:             db="dbname",
11:             passwd="password")
12:         return db
13:     except MySQLdb.Error, e:
14:         print '<p>Connection failed.<br>'
15:         print e
16:         print '</body></html>'
17:         return null
18: 
19: if __name__ == '__main__':
20: 
21: 
22:     import cgitb; # CGI traceback module
23:     cgitb.enable()
24: 
25:     print "Content-Type: Text/html"
26:     print
27:     print '''<html>
28: <head><title>MySQL Database Connection Test</title></head>
29: <body>
30: <h1>MySQL Database Connection Test</h1>'''
31: 
32:     db = dbopen();
33:     if (not db):
34:         import sys
35:         sys.exit(1)
36:     cursor = db.cursor()
37:     cursor.execute ("DESCRIBE users")
38:     rows = cursor.fetchall()
39:     if (cursor.rowcount>0):
40:         print '<p><pre>'
41:     for row in rows:
42:         print row
43:     print '</pre>'
44:     print '</body></html>'
45:     cursor.close()
46:     db.close()


Maintained by John Loomis, updated Sun Mar 02 20:58:25 2008