Skip to content

Instantly share code, notes, and snippets.

@Odame
Forked from robcowie/mysqldb_query_generator.py
Created June 22, 2017 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Odame/4fa4e02619cb234c418a64fe84373002 to your computer and use it in GitHub Desktop.
Save Odame/4fa4e02619cb234c418a64fe84373002 to your computer and use it in GitHub Desktop.
Memory-efficient, streaming query generator with MySQLdb
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