944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3021
  • Python RSS
Oct 21st, 2006
0

python mysql doesnt work?plz help

Expand Post »
this is a program that i have, i dont know why it doesnt work. Could some one help me? thank you very much
Python Syntax (Toggle Plain Text)
  1.  
  2. #!c:\Python24\python.exe
  3. # Fig. 35.22: fig35_22.py
  4. # A program to illustrate Python's database connectivity.
  5. import MySQLdb
  6.  
  7. print "Content-type: text/html"
  8. print
  9. print """
  10. <html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"
  11. lang="en">
  12. <head><title>Select Author</title></head>
  13. <body style =
  14. font-family: Arial, sans-serif; font-size: 11pt">"""
  15.  
  16. try:
  17. connection = MySQLdb.connect(host = "localhost", user = "student", passwd = "student", db = "books")
  18. except OperationalError:
  19.  
  20. print "Unable to connect to database: %s" % message
  21. else:
  22. cursor = connection.cursor()
  23. cursor.execute( "SELECT * from Authors" )
  24. authorList = cursor.fetchall()
  25.  
  26. cursor.close() # close cursor
  27. connection.close() # close connection
  28.  
  29. print """
  30. <form method = "post" action = "/cgi-bin/fig35_23.py">
  31. <select name = "authorID">"""
  32.  
  33. for author in authorList:
  34. print """<option value = %d>%s, %s</option>""" \
  35. % ( author[ 0 ], author[ 2 ], author[ 1 ] )
  36.  
  37. print """
  38. </select>
  39. <input type = "submit" value = "Execute Query" />
  40. </ form>"""
  41.  
  42. print """</body></html>"""
  43.  

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)
  1. #!c:\Python24\python.exe
  2. # Fig. 35.22: fig35_22.py
  3. # A program to illustrate Python's database connectivity.
  4. import MySQLdb
  5. print "Content-type: text/html"
  6. print
  7. print """
  8. <html xmlns = "<a rel="nofollow" href="http://www.w3.org/1999/xhtml" target="_blank">http://www.w3.org/1999/xhtml</a>" xml:lang="en"
  9. lang="en">
  10. <head><title>Select Author</title></head>
  11. <body style =
  12. font-family: Arial, sans-serif; font-size: 11pt">"""
  13.  
  14. try:
  15. connection = MySQLdb.connect(host = "localhost", user = "root", passwd = "student", db = "books")
  16. except:
  17. print "Unable to connect to database: "
  18. else:
  19. cursor = connection.cursor()
  20. cursor.execute( "SELECT * from Authors" )
  21. authorList = cursor.fetchall()
  22. cursor.close() # close cursor
  23. connection.close() # close connection
  24.  
  25. print """
  26. <form method = "post" action = "/cgi-bin/fig35_23.py">
  27. <select name = "authorID">"""
  28. for author in authorList:
  29. print """<option value = %d>%s, %s</option>""" \
  30. % ( author[ 0 ], author[ 2 ], author[ 1 ] )
  31. print """
  32. </select>
  33. <input type = "submit" value = "Execute Query" />
  34. </ form>"""
  35. print """</body></html>"""
  36.  

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dummies2 is offline Offline
2 posts
since Oct 2006
Oct 22nd, 2006
0

Re: python mysql doesnt work?plz help

I keep reading that MySQL is hard to program with, no matter what computer language you use. Hope you find someone familiar with MySQL.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jun 13th, 2007
0

Re: python mysql doesnt work?plz help

> what is wrong with the code.

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.
Reputation Points: 94
Solved Threads: 48
Posting Whiz
BearofNH is offline Offline
321 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Need Help - Python Question
Next Thread in Python Forum Timeline: Where's Nemo?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC