Created
February 7, 2011 16:05
-
-
Save robcowie/814599 to your computer and use it in GitHub Desktop.
Memory-efficient, streaming query generator with MySQLdb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from MySQLdb.cursors import SSDictCursor | |
def iterate_query(query, connection, arraysize=1): | |
c = connection.cursor(cursorclass=SSDictCursor) | |
c.execute(query) | |
while True: | |
nextrows = c.fetchmany(arraysize) | |
if not nextrows: | |
break | |
for row in nextrows: | |
yield row | |
c.close() | |
results = iterate_query(SQL, conn, arraysize=100) | |
for row_dict in results: | |
print row_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment