943,757 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1339
  • Python RSS
Jan 28th, 2009
0

print out some result

Expand Post »
I am trying to print out some database resutls as follows


Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  1.  
  2. print Results_user[0]["ID"]

i get info printed out. Why wont it work with the loop
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008
Jan 28th, 2009
0

Re: print out some result

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.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jan 29th, 2009
0

Re: print out some result

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

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008
Jan 29th, 2009
0

Re: print out some result

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

Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008

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: Python and Flash AS3
Next Thread in Python Forum Timeline: wxPython help





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


Follow us on Twitter


© 2011 DaniWeb® LLC