this is a program that i have, i dont know why it doesnt work. Could some one help me? thank you very much
#!c:\Python24\python.exe
# Fig. 35.22: fig35_22.py
# A program to illustrate Python's database connectivity.
import MySQLdb
print "Content-type: text/html"
print
print """
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head><title>Select Author</title></head>
<body style =
font-family: Arial, sans-serif; font-size: 11pt">"""
try:
connection = MySQLdb.connect(host = "localhost", user = "student", passwd = "student", db = "books")
except OperationalError:
print "Unable to connect to database: %s" % message
else:
cursor = connection.cursor()
cursor.execute( "SELECT * from Authors" )
authorList = cursor.fetchall()
cursor.close() # close cursor
connection.close() # close connection
print """
<form method = "post" action = "/cgi-bin/fig35_23.py">
<select name = "authorID">"""
for author in authorList:
print """<option value = %d>%s, %s</option>""" \
% ( author[ 0 ], author[ 2 ], author[ 1 ] )
print """
</select>
<input type = "submit" value = "Execute Query" />
</ form>"""
print """</body></html>"""
i get an error at OperationalError. It says OperationalError is undefined. now if i take out OperationalError it wont work because of the line......%s" % message inside that try and catch function. Now if i take those two codes then i will get it to have the drop down list but there is nothing in that drop down list. This is the code i have
#!c:\Python24\python.exe
# Fig. 35.22: fig35_22.py
# A program to illustrate Python's database connectivity.
import MySQLdb
print "Content-type: text/html"
print
print """
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head><title>Select Author</title></head>
<body style =
font-family: Arial, sans-serif; font-size: 11pt">"""
try:
connection = MySQLdb.connect(host = "localhost", user = "student", passwd = "student", db = "books")
except:
print "Unable to connect to database: "
else:
cursor = connection.cursor()
cursor.execute( "SELECT * from Authors" )
authorList = cursor.fetchall()
cursor.close() # close cursor
connection.close() # close connection
print """
<form method = "post" action = "/cgi-bin/fig35_23.py">
<select name = "authorID">"""
for author in authorList:
print """<option value = %d>%s, %s</option>""" \
% ( author[ 0 ], author[ 2 ], author[ 1 ] )
print """
</select>
<input type = "submit" value = "Execute Query" />
</ form>"""
print """</body></html>"""
I'm using pythong to connect to mysql. here is my table structure for books database
create table authors(
authorID char(3),
firstName varchar(20),
lastName varchar(20)
insert into authors values('1','Harvey','Deitel');
insert into authors values('2','Paul','Deitel');
insert into authors values('3','Temn','Nieto');
insert into authors values('4','Kate','Steinbuhler');
insert into authors values('5','Sean','Santry');
insert into authors values('6','Ted','Linn');
insert into authors values('7','Praveen','Sadhu');
insert into authors values('8','David','McPhie');
insert into authors values('9','Cheryl','Yaeger');
insert into authors values('10','Marina','Zlatkina');
insert into authors values('11','Ben','Wiedernann');
insert into authors values('12','Jonathan','Liperi');
pleaze help me. thank you very much.
);