How to use Python list comprehension (or such) for retrieving rows when using MySQLdb?

Posted by Erik Nygren on Stack Overflow See other posts from Stack Overflow or by Erik Nygren
Published on 2010-04-15T22:41:34Z Indexed on 2010/04/15 22:43 UTC
Read the original article Hit count: 534

Filed under:
|
|

Hey all,

I use MySQLdb a lot when dealing with my webserver. I often find myself repeating the lines:

row = cursor.fetchone()
while row:
    do_processing(row)
    row = cursor.fetchone()

Somehow this strikes me as somewhat un-pythonic. Is there a better, one-line way to accomplish the same thing, along the lines of inline assignment in C:

while (row = do_fetch()) { 
    do_processing(row); 
} 

I've tried figuring out the syntax using list comprehensions, but I can't seem to figure it out. Any recommendations?

Thanks,

Erik

© Stack Overflow or respective owner

Related posts about python

Related posts about mysqldb