python mysql doesnt work?plz help

Reply

Join Date: Oct 2006
Posts: 2
Reputation: dummies2 is an unknown quantity at this point 
Solved Threads: 0
dummies2 dummies2 is offline Offline
Newbie Poster

python mysql doesnt work?plz help

 
0
  #1
Oct 21st, 2006
this is a program that i have, i dont know why it doesnt work. Could some one help me? thank you very much
  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

  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" class="t" 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,222
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: python mysql doesnt work?plz help

 
0
  #2
Oct 22nd, 2006
I keep reading that MySQL is hard to program with, no matter what computer language you use. Hope you find someone familiar with MySQL.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: python mysql doesnt work?plz help

 
0
  #3
Jun 13th, 2007
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC