Paged queries
Read a large SQLite result one page at a time with a query cursor.
A SQLite statement can pause after returning a row and resume the next time it is stepped. If it reads a table, its first step opens a read transaction. The transaction keeps the same view of committed data across pages, even if a writer commits changes between them.
Use openCursor() to process a large result in pages. Nitro SQLite prepares and binds the statement once. Each call to readPage() steps up to the requested number of rows and copies only that page to JavaScript. For queries that sort or aggregate, SQLite can still do work or allocate temporary storage before returning the first row. The page size bounds the row count in each result, so a page with large BLOBs can still take substantial memory.
Read a result in pages
import { open } from 'react-native-nitro-sqlite'
const db = open({ name: 'app.sqlite' })
const cursor = await db.openCursor<{ id: number; title: string }>(
'SELECT id, title FROM articles WHERE published = ? ORDER BY id',
{ params: [true], pageSize: 128 },
)
try {
while (true) {
const page = await cursor.readPage()
for (const article of page.rows) {
console.log(article.id, article.title)
}
if (page.done) break
}
} finally {
await cursor.close()
}Request pages one at a time. The default page size is 256, and you can set it from 1 to 4096. A full page always has done: false. If the row count is an exact multiple of your page size, the last request returns an empty page with done: true. The last page of a partial result can include rows, so process its rows before checking done.
Every page includes columns in SQL column order, even if it has no rows. Repeated column names appear in columns. In a row object, the last value for a repeated name wins. Nitro SQLite copies BLOBs out of SQLite, so they remain usable in JavaScript after the cursor closes.
Choose when to close the cursor
Use a finally block to close the cursor when you stop early or processing throws. Exhaustion and SQLite errors also release the connection. If a page is pending, close() waits for the request to finish before releasing the connection. Closing again is safe. Garbage collection can release an abandoned cursor, but its timing is unpredictable.
The cursor uses a private, read-only connection to the main database file. It requires a thread-safe SQLite build. You can read while a write transaction is open, and the cursor sees committed data as of its first read. Temporary tables, attachments, and connection-local settings on the source connection are unavailable. Use one read-only SQL query that returns rows. SQLite pragmas and transaction control statements are rejected.
Closing the source connection leaves the cursor open. To delete the database, close its cursors first. In WAL mode, writes can continue while a cursor holds its snapshot, but the snapshot can prevent a checkpoint from truncating the WAL file. With rollback journaling, a reader can delay a writer's commit. Keep readers short when the database is also receiving writes. See Isolation and concurrency.