Python Forum
loop through list made from query - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: loop through list made from query (/thread-13600.html)



loop through list made from query - zxcv - Oct-23-2018

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()



RE: loop through list made from query - stullis - Oct-23-2018

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.