| | |
print out some result
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
I am trying to print out some database resutls as follows
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
i get info printed out. Why wont it work with the loop
Python Syntax (Toggle Plain Text)
cursor.execute("SELECT * FROM User") numrows_user = int(cursor.rowcount) i_user = int(0) Results_user = cursor.fetchall() while i_user < numrows_user: 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)
print Results_user[0]["ID"]
i get info printed out. Why wont it work with the loop
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.
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
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
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.
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)
#drop down boxes #execute SQL select statement cursor.execute("SELECT * FROM User") numrows_user = int(cursor.rowcount) i_user = 0 Results_user = cursor.fetchall() while i_user < numrows_user: for element in Results_user[i_user].keys(): if element == "UserName": if Results_user[i_user][element] != Results[i][subelement]: print "<option value=\""+Results_user[i_user][element]+"\">"+Results_user[i_user][element]+"</option>" i_user = i_user + 1 else: i_user = i_user + 1 continue else: continue print "</select>" 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.
•
•
Join Date: Nov 2008
Posts: 58
Reputation:
Solved Threads: 0
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
any ideas?
Python Syntax (Toggle Plain Text)
#!/usr/bin/python import MySQLdb import os import Cookie import time print "Content-type: text/html\n" # connect db = MySQLdb.connect(host = "localhost", user = "dfgfdgfdgdfgdfg", passwd = "fdgdfgfd", db = "afgfdgdfgfdgdfg") # create a database cursor cursor = db.cursor(MySQLdb.cursors.DictCursor) #execute SQL select statement cursor.execute("SELECT * FROM on_call") numrows = int(cursor.rowcount) Results = cursor.fetchall() #getting all users name cursor.execute("SELECT UserName, ID FROM User") numrows = int(cursor.rowcount) Result = cursor.fetchall() row = cursor.rowcount i = 0 print "<form action=\"update_contact_schedule.py\" method=\"post\">" for subelement in Results[0].keys(): i = 0 if subelement == "Monday": print "Monday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Monday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" i = i + 1 print "</select>" print "<br />" elif subelement == "Tuesday": print "Tuesday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Tuesday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" elif subelement == "Wednesday": print "Wednesday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Wednesday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" elif subelement == "Thursday": print "Tursday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Thursday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" elif subelement == "Friday": print "Friday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Friday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" elif subelement == "Saturday": print "Saturday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Saturday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" elif subelement == "Sunday": print "Sunday:" #print whos currently on call for the day cursor.execute("SELECT UserName FROM User WHERE ID =" + Results[0][subelement]) User = cursor.fetchall() print "<select name=\"Sunday\">" print "<option value=\""+Results[0][subelement]+"\">"+User[0]["UserName"]+"</option>" while i < row: if Result[i]["UserName"] == User[0]["UserName"]: i = i + 1 else: print "<option value=\"" print Result[i]["ID"] print "\">" print Result[i]["UserName"] print "</option>" print "<br />" i = i + 1 print "</select>" print "<br />" print "<br />" print "<br />" print "<input type=\"submit\" value=\"Submit\">" print "</form>"
any ideas?
![]() |
Similar Threads
- Can't Print Out Result of Multiplication Of Hex Numbers (Assembly)
- Debug & Debug.Print (Visual Basic 4 / 5 / 6)
- newb: Print ids, you get 1L, what is L? (Python)
- How to print a single character in a specific place in a line? (C)
- print odd and even numbers (Assembly)
- High-Level Languages (Computer Science)
- internet explorer while trying to print (Web Browsers)
- Adding data to print job (Visual Basic 4 / 5 / 6)
Other Threads in the Python Forum
- Previous Thread: Python and Flash AS3
- Next Thread: wxPython help
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt





