I'm trying to implement a pagination build in Room database android, and I need to have the clause OFFSET
and FETCH NEXT
which is not alowing me to use, what's the way to implement a query using these clauses. Did some research but no results gained. The SQL query I'm trying to implement on ROOM dao looks like this:
SELECT * FROM Persons ORDER BY id OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
You can use OFFSET without FETCH, but FETCH can't be used by itself. Regardless, OFFSET must be used with an ORDER BY clause. The reason is simple as OFFSET and FETCH are part of the ORDER BY clause. In this example the first ten rows of the result are skipped, then the next 10 displayed in the result.
you can use limit without order by.
OFFSET and FETCH return the set of records in a query. OFFSET is used to skip the number of rows, and FETCH provides the number of rows we want to return after the OFFSET in the result. TOP is not ANSI-compliant, so it can only be used with Microsoft products like SQL Server and MS-Access.
This is how I managed to achieve and it's working just fine:
(Kotlin sample)
SELECT * FROM Persons WHERE name LIKE :param LIMIT :pageSize OFFSET :pageIndex
where:
PARAM = search criteria,
PAGESIZE = number of rows to return per page,
PAGEINDEX = what page to return
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With