Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Simple and concise way to handle cursors in a DB query

xnopasaranx edited this page Nov 8, 2019 · 4 revisions

Our documentation has a different example right now, where the query is run in a seperate method. If we want to run a query inside of a method, here is a simple way to pass a cursor:

def getExampleEntries(self, *args, **kwargs):
    query = exampleSkel().all()
    query.order("creationdate")

    # initialize cursor as None
    cursor = None
    ret = []

    # while query is running
    while True:
        query.cursor(cursor)
        res = query.fetch()

        # nothing in res means we are done and can break out of while loop
        if not res:
            break

        # if we are still running, there must be a cursor to pass and continue with next chunk
        cursor = query.getCursor()
        ret.extend(res)

    # return our query results
    return ret

This is a concise way to handle queries with more than 99 results

Clone this wiki locally