Python Forum
loop through list made from query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop through list made from query
#1
New to Python.
Need to loop through query results like
col1,col2
---------
'' , 123
'' , 23
'' , 200
'a' , 33
'a' , 1
'b' , 0
'b' , 1
'c' , 2.

How do you do something like this "pseudo code"
while (not at end of rows)
   col=col1
   while (col=col1 and not at end of rows)
      do something
      go to next row
   endwhile
endwhile
Am doing this now, but it doesn't depend on the value of col1
thecursor.execute(thequerystring)
queryvalues=thecursor.fetchall()
count=0
if (len(queryvalues)>0):
   for row in queryvalues:
      count=count+1
      dosomething()
Reply
#2
I suppose you intend to do something with the value of column 1. To do that, you would have to pass that value into your function call:

thecursor.execute(thequerystring)
queryvalues=thecursor.fetchall()
count=0
if (len(queryvalues)>0):
   for row in queryvalues:
      count=count+1
      dosomething(row[0]) #Python is 0-indexed
I'm not sure what you're doing with your count variable either. It may not be necessary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list the files using query in python arjunaram 0 649 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 2,884 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
  Appending to list of list in For loop nico_mnbl 2 2,320 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,311 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  loop through list or double loop 3Pinter 4 3,386 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,716 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,893 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,147 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020