NitroSQLite
Guides

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. Opening is queued behind operations already submitted to the native source worker. It does not wait for operations still queued in JavaScript, such as a write waiting behind a managed transaction. Await any writes or transactions whose results the cursor must include. Once open, page reads use the cursor's own worker. In WAL mode, you can read while a write transaction is open, and the cursor sees committed data as of its first read. In rollback-journal mode, an exclusive or pending writer lock prevents a new reader from opening or reading. 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 partial cursor holds a read lock. By default, conflicting writes and commits fail immediately with SQLITE_BUSY until the cursor closes or finishes. A writer's busy_timeout only waits for a bounded time and can still fail. It does not release the cursor's lock. Keep readers short when the database is also receiving writes. See Isolation and concurrency.

Handle lock contention

Cursor preparation and page errors expose SQLite's result code through NitroSQLiteError.sqliteCode. Primary code 5 is SQLITE_BUSY and 6 is SQLITE_LOCKED. Read the primary code with error.sqliteCode & 0xff after checking that the code is defined. The reader has no busy timeout and does not inherit the source's timeout. Release the conflicting lock before retrying. A SQLite error while reading a page closes its cursor, so retry that query by opening a new cursor. A request rejected before it reaches the worker does not necessarily release the reader. Always call close() in finally. If submitting close() fails, retry close(); until cleanup succeeds, the snapshot can remain live even though further reads are rejected.

Use encrypted databases

With a licensed SEE build, the managed cursor reuses the encryptionKey supplied to open(). That managed connection retains the key in JavaScript for later cursor creation. Raw native callers must supply the current key themselves. The key is not copied from the source handle, and an arbitrary PRAGMA rekey does not update the managed connection's saved key. Close cursors before changing the key, then close and reopen the source with the new key before opening more cursors. See Encryption.

On this page