How can I perform a SQLITE search that returns a maximum number of records that also has a WHERE clause?

I have a method that can return items from a database in blocks of ‘numRecords’ (e.g. 100)
It works by recording the offset to begin searching from based on previous returned items, assuming that all records will be returned. All this is encapsulated in a class called ‘Walker’

Now I want to add a WHERE clause to the search, so this algorithm will not work since not all records will be returned

   class Walker:
        current_offset  = 0;

        def getNextRecords(cls, numRecords):
           cursor = cls.database.cursor()

           cmd=("SELECT *"
                + " FROM "
                + DB_TABLE
                + " LIMIT " + str(numRecords)
                + " OFFSET " + str(cls.current_offset)
                )
           cursor.execute(cmd)
           items = cursor.fetchall()
           cursor.close()

        cls.current_offset += len(items)

        return items

How can I return sequential sets of ‘numRecords’ records from the database while honoring a WHERE clause?

EDIT: what I am looking for is a method that will return the first N items that match the WHERE clause, and on the next call return the next N items that match the query

  • 1

    The question is unclear. The WHERE clause won’t break paging.

    – 

  • @PanagiotisKanavos I tried to clarify the question in an edit – thanks

    – 

  • I repeat, WHERE doesn’t break paging. If you search for Red cars, WHERE Color='Red' ORDER BY ID LIMIT 50 OFFSET 0 will return the first 50 cars, ORDER BY ID LIMIT 50 OFFSET 50 the next 50 etc. Ordering is important, because that determines which 50 items are the first and allows skipping them with OFFSET

    – 

Leave a Comment