Web Programming: Henning Schulzrinne Dept. of Computer Science Columbia University
Web Programming: Henning Schulzrinne Dept. of Computer Science Columbia University
Henning Schulzrinne
Dept. of Computer Science
Columbia University
print "</body>"
try:
db = connect(host='grandcentral',
user='cs3995', passwd='cs3995',
db='grades')
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
c = db.cursor()
c.execute("SELECT ... FROM ...")
results = c.fetchall() # list of tuples
c.close()
Jun 23, 2024 Advanced Programming 30
Spring 2002
SQL Python interface
Results are just tuples, with fields in
order of table definition
can also fetch one row at a time:
c.execute("SELECT firstname,lastname FROM
students ORDER BY lastname")
print "<ul>"
while (1):
student = c.fetchone()
if student == None: break
print "<li>", student, student[0]
print "</ul>"