print out some result

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

print out some result

 
0
  #1
Jan 28th, 2009
I am trying to print out some database resutls as follows


  1. cursor.execute("SELECT * FROM User")
  2. numrows_user = int(cursor.rowcount)
  3. i_user = int(0)
  4. Results_user = cursor.fetchall()
  5.  
  6.  
  7.  
  8. while i_user < numrows_user:
  9. print Results_user[i_user]["ID"]

Put i am getting nothing i know i_user is working and i know there is information in the database if i do something like

  1.  
  2. print Results_user[0]["ID"]

i get info printed out. Why wont it work with the loop
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: print out some result

 
0
  #2
Jan 28th, 2009
what happens when you run the code? it "should" hit the while and continuously print (you have an infinite loop there). if you are not getting into a infinite loop, numrows_user is probably not set to what you expect.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: print out some result

 
0
  #3
Jan 29th, 2009
its not an infinite loop as numrows_user is part of a database search earlier on and when i print it out it = 4.

Whats supposed to happen when i run the code is the 4 ID from a data base table is printed out. This is the whole code i am trying to run

  1. #drop down boxes
  2. #execute SQL select statement
  3. cursor.execute("SELECT * FROM User")
  4. numrows_user = int(cursor.rowcount)
  5. i_user = 0
  6. Results_user = cursor.fetchall()
  7.  
  8.  
  9.  
  10. while i_user < numrows_user:
  11. for element in Results_user[i_user].keys():
  12. if element == "UserName":
  13. if Results_user[i_user][element] != Results[i][subelement]:
  14. print "<option value=\""+Results_user[i_user][element]+"\">"+Results_user[i_user][element]+"</option>"
  15. i_user = i_user + 1
  16. else:
  17. i_user = i_user + 1
  18. continue
  19. else:
  20. continue
  21. print "</select>"
  22. print "<br />"

The above code actually works but i need to change Results_user[i_user][element] to Results_user[i_user]["ID"] and thats when i get nothing

when i try and print out Results_user[i_user][element] it works but when i specify element i get nothing. But if i do Results_user[0]["ID"] i get the one result printed out.
Last edited by adam291086; Jan 29th, 2009 at 4:08 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: print out some result

 
0
  #4
Jan 29th, 2009
ok i have re writtent the code and its now working but the code is sloppy and there must be a better way of doing this

  1. #!/usr/bin/python
  2. import MySQLdb
  3. import os
  4. import Cookie
  5. import time
  6. print "Content-type: text/html\n"
  7. # connect
  8. db = MySQLdb.connect(host = "localhost", user = "dfgfdgfdgdfgdfg", passwd = "fdgdfgfd", db = "afgfdgdfgfdgdfg")
  9.  
  10. # create a database cursor
  11. cursor = db.cursor(MySQLdb.cursors.DictCursor)
  12.  
  13. #execute SQL select statement
  14. cursor.execute("SELECT * FROM on_call")
  15. numrows = int(cursor.rowcount)
  16. Results = cursor.fetchall()
  17.  
  18. #getting all users name
  19. cursor.execute("SELECT UserName, ID FROM User")
  20. numrows = int(cursor.rowcount)
  21. Result = cursor.fetchall()
  22. row = cursor.rowcount
  23. i = 0
  24.  
  25.  
  26.  
  27. print "<form action=\"update_contact_schedule.py\" method=\"post\">"
  28.  
  29. for subelement in Results[0].keys():
  30. i = 0
  31. if subelement == "Monday":
  32. print "Monday:"
  33.  
  34. #print whos currently on call for the day
  35. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  36. User = cursor.fetchall()
  37. print "<select name=\"Monday\">"
  38. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  39. while i < row:
  40. if Result[i]["UserName"] == User[0]["UserName"]:
  41. i = i + 1
  42. else:
  43.  
  44. print "<option value=\""
  45. print Result[i]["ID"]
  46. print "\">"
  47. print Result[i]["UserName"]
  48. print "</option>"
  49. i = i + 1
  50. print "</select>"
  51. print "<br />"
  52.  
  53.  
  54. elif subelement == "Tuesday":
  55. print "Tuesday:"
  56.  
  57. #print whos currently on call for the day
  58. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  59. User = cursor.fetchall()
  60. print "<select name=\"Tuesday\">"
  61. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  62. while i < row:
  63. if Result[i]["UserName"] == User[0]["UserName"]:
  64. i = i + 1
  65. else:
  66.  
  67. print "<option value=\""
  68. print Result[i]["ID"]
  69. print "\">"
  70. print Result[i]["UserName"]
  71. print "</option>"
  72. print "<br />"
  73. i = i + 1
  74. print "</select>"
  75. print "<br />"
  76.  
  77. elif subelement == "Wednesday":
  78. print "Wednesday:"
  79.  
  80. #print whos currently on call for the day
  81. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  82. User = cursor.fetchall()
  83. print "<select name=\"Wednesday\">"
  84. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  85. while i < row:
  86. if Result[i]["UserName"] == User[0]["UserName"]:
  87. i = i + 1
  88. else:
  89.  
  90. print "<option value=\""
  91. print Result[i]["ID"]
  92. print "\">"
  93. print Result[i]["UserName"]
  94. print "</option>"
  95. print "<br />"
  96. i = i + 1
  97. print "</select>"
  98. print "<br />"
  99.  
  100. elif subelement == "Thursday":
  101. print "Tursday:"
  102.  
  103. #print whos currently on call for the day
  104. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  105. User = cursor.fetchall()
  106. print "<select name=\"Thursday\">"
  107. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  108. while i < row:
  109. if Result[i]["UserName"] == User[0]["UserName"]:
  110. i = i + 1
  111. else:
  112.  
  113. print "<option value=\""
  114. print Result[i]["ID"]
  115. print "\">"
  116. print Result[i]["UserName"]
  117. print "</option>"
  118. print "<br />"
  119. i = i + 1
  120. print "</select>"
  121. print "<br />"
  122.  
  123. elif subelement == "Friday":
  124. print "Friday:"
  125.  
  126. #print whos currently on call for the day
  127. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  128. User = cursor.fetchall()
  129. print "<select name=\"Friday\">"
  130. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  131. while i < row:
  132. if Result[i]["UserName"] == User[0]["UserName"]:
  133. i = i + 1
  134. else:
  135.  
  136. print "<option value=\""
  137. print Result[i]["ID"]
  138. print "\">"
  139. print Result[i]["UserName"]
  140. print "</option>"
  141. print "<br />"
  142. i = i + 1
  143. print "</select>"
  144. print "<br />"
  145.  
  146. elif subelement == "Saturday":
  147. print "Saturday:"
  148.  
  149. #print whos currently on call for the day
  150. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  151. User = cursor.fetchall()
  152. print "<select name=\"Saturday\">"
  153. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  154. while i < row:
  155. if Result[i]["UserName"] == User[0]["UserName"]:
  156. i = i + 1
  157. else:
  158.  
  159. print "<option value=\""
  160. print Result[i]["ID"]
  161. print "\">"
  162. print Result[i]["UserName"]
  163. print "</option>"
  164. print "<br />"
  165. i = i + 1
  166. print "</select>"
  167. print "<br />"
  168.  
  169.  
  170. elif subelement == "Sunday":
  171. print "Sunday:"
  172.  
  173. #print whos currently on call for the day
  174. cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement])
  175. User = cursor.fetchall()
  176. print "<select name=\"Sunday\">"
  177. print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>"
  178. while i < row:
  179. if Result[i]["UserName"] == User[0]["UserName"]:
  180. i = i + 1
  181. else:
  182.  
  183. print "<option value=\""
  184. print Result[i]["ID"]
  185. print "\">"
  186. print Result[i]["UserName"]
  187. print "</option>"
  188. print "<br />"
  189. i = i + 1
  190. print "</select>"
  191. print "<br />"
  192.  
  193.  
  194. print "<br />"
  195. print "<br />"
  196. print "<input type=\"submit\" value=\"Submit\">"
  197. print "</form>"

any ideas?
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