Handling large ResultSets

Introduction to Databases in Python

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Dealing with large ResultSets

  • fetchmany() lets us specify how many rows we want to act upon
  • We can loop over fetchmany()
  • It returns an empty list when there are no more records
  • We have to close the ResultProxy afterwards
Introduction to Databases in Python

Fetching many rows

while more_results:

partial_results = results_proxy.fetchmany(50)
if partial_results == []: more_results = False
for row in partial_results: state_count[row.state] += 1
results_proxy.close()
Introduction to Databases in Python

Let's practice!

Introduction to Databases in Python

Preparing Video For Download...