Assignment in a while?

Ben Thul thulben at no-spam.com
Sun Apr 2 19:48:42 EDT 2006


The inline iterator version fits very well with my sensibilities.  The 
problem that I have with fetchall is that sometimes you need to deal 
with a very large dataset.  Calling fetchall() on it will put the whole 
thing in memory, which is no good.  Better to iterate over it one row at 
a time, IMO.

Thanks a ton!
Ben

Duncan Booth wrote:
> none wrote:
> 
> 
>>If I try to run the above code, I get a SyntaxError indicating that I 
>>can't do an assignment in the while loop.  I found a way around this 
>>(see the commented out while loop), but it seems hackish.  Assignment 
>>within a while loop seems like a pretty standard thing, so I'm just 
>>curious what I'm missing.
>>
> 
> 
> Not much, you cannot assign to a variable in the controlling expression 
> of a while loop. However, for loops do assign values to variables, so if 
> the form with the break offends you, try restructuring your code as a for 
> loop. For example:
> 
> for results in iter(sth.fetchone, None):
>    print results
> 
> or in many cases you can just fetch everything in one go:
> 
> for results in sth.fetchall():
>    print results



More information about the Python-list mailing list