| | |
python mysql doesnt work?plz help
![]() |
•
•
Join Date: Oct 2006
Posts: 2
Reputation:
Solved Threads: 0
this is a program that i have, i dont know why it doesnt work. Could some one help me? thank you very much
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
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.
);
then in the end it should have a drop down list that contains
id, fname, lname like this
1, Harvey, Deitel
2,.....................
plz help me the error i got now from error log in apache is
" File "C:/Programfiles/Apache2.2/cgi-bin/fig35_22.py", line 41\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] print """</body></html>"""\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] ^\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] SyntaxError: invalid syntax\r
what is wrong with the code. When i open the page http://localhost/cgi-bin/filename.py i see the drop down list but inside is empty and no "Execute Querry" button . plz help me
Python Syntax (Toggle Plain Text)
#!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 """ <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
Python Syntax (Toggle Plain Text)
#!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 """ <html xmlns = "<a rel="nofollow" class="t" href="http://www.w3.org/1999/xhtml" target="_blank">http://www.w3.org/1999/xhtml</a>" 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 = "root", 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.
);
then in the end it should have a drop down list that contains
id, fname, lname like this
1, Harvey, Deitel
2,.....................
plz help me the error i got now from error log in apache is
" File "C:/Programfiles/Apache2.2/cgi-bin/fig35_22.py", line 41\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] print """</body></html>"""\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] ^\r
[Thu Oct 19 22:24:18 2006] [error] [client 111.1.1.0] SyntaxError: invalid syntax\r
what is wrong with the code. When i open the page http://localhost/cgi-bin/filename.py i see the drop down list but inside is empty and no "Execute Querry" button . plz help me
Last edited by dummies2; Oct 21st, 2006 at 7:55 pm.
> what is wrong with the code.
Several things, actually. What is the stray quote doing in:
But I suspect the real problem is here:
You are treating author[ 0 ] like an integer (with the %d format) but your table definition lists it as a char(3), and you are (correctly) inserting string values. Ergo, a quick switch from %d to %s should solve the problem. I was able to get the form generated properly, including the "Execute Query" button.
I have had only good experiences combining Python (2.4+) and MySQL (5.0+). SQL can be a pain sometimes but the MySQL.com newbie forum has always been helpful to me, for well-phrased questions.
Several things, actually. What is the stray quote doing in:
font-family: Arial, sans-serif; font-size: 11pt"> ?But I suspect the real problem is here:
print """<option value = %d>%s, %s</option>""" \
% ( author[ 0 ], author[ 2 ], author[ 1 ] )You are treating author[ 0 ] like an integer (with the %d format) but your table definition lists it as a char(3), and you are (correctly) inserting string values. Ergo, a quick switch from %d to %s should solve the problem. I was able to get the form generated properly, including the "Execute Query" button.
I have had only good experiences combining Python (2.4+) and MySQL (5.0+). SQL can be a pain sometimes but the MySQL.com newbie forum has always been helpful to me, for well-phrased questions.
![]() |
Similar Threads
- Internet explorer pop up blocker doesnt work (Web Browsers)
- what doesnt work? plz help guys (C++)
- My Cd DRive doesnt work PLZ HELP (Storage)
- first website doesnt work well? (HTML and CSS)
- Creative webcam doesnt work!!! HELP!!!!! (USB Devices and other Peripherals)
- My phpbb forum doesnt work ? (PHP)
Other Threads in the Python Forum
- Previous Thread: Need Help - Python Question
- Next Thread: Where's Nemo?
| Thread Tools | Search this Thread |
alarm ansi anydbm app assignment backend beginner binary bluetooth character cipher cmd coordinates customdialog cx-freeze data decimals development directory dynamic exe feet file float format function generator getvalue gnu graphics halp handling heads homework http ideas input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame pymailer python queue random recursion recursive schedule screensaverloopinactive script slicenotation sqlite ssh statistics string strings sudokusolver text thread time tlapse tuple ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib xlwt






