Skip to main content

expo-sqlite

Version: 55.0.6 Provides access to a database using SQLite. The database is persisted across restarts of your app. Offers a modern promise-based API with support for transactions, prepared statements, and more.

Installation

Usage

API Reference

Database Methods

(databaseName: string, options?: SQLiteOpenOptions) => Promise<SQLiteDatabase>
Opens or creates a SQLite databaseParameters:
  • databaseName (string): Database file name
  • options (SQLiteOpenOptions): Optional configuration
(databaseName: string, options?: SQLiteOpenOptions) => SQLiteDatabase
Synchronous version of openDatabaseAsync
(databaseName: string) => Promise<void>
Deletes a database file
(databaseName: string) => void
Synchronous version of deleteDatabaseAsync

SQLiteDatabase

Query Methods

(query: string, ...params: any[]) => Promise<any[]>
Executes a query and returns all rows
(query: string, ...params: any[]) => Promise<any | null>
Executes a query and returns the first row
(query: string, ...params: any[]) => Promise<SQLiteRunResult>
Executes a query without returning rowsReturns object with lastInsertRowId and changes
(source: string) => Promise<void>
Executes multiple SQL statements

Prepared Statements

(query: string) => Promise<SQLiteStatement>
Creates a prepared statement for efficient repeated execution

Transactions

(task: () => Promise<void>) => Promise<void>
Executes operations within a transactionAutomatically commits on success or rolls back on error
(task: () => Promise<void>) => Promise<void>
Executes operations within an exclusive transactionGuarantees no other queries interrupt the transaction

Other Methods

() => Promise<void>
Closes the database connection
() => Promise<boolean>
Checks if database is currently in a transaction
(databaseName?: string) => Promise<Uint8Array>
Serializes database to Uint8Array

SQLiteStatement

(...params: any[]) => Promise<SQLiteExecuteAsyncResult>
Executes the prepared statement
() => Promise<void>
Finalizes and releases the statement

Hooks

() => SQLiteDatabase
Access database from SQLiteProvider context

Types

SQLiteRunResult

number
ID of the last inserted row
number
Number of rows changed

Examples

Basic CRUD Operations

Using Prepared Statements

Transactions

Complex Queries

React Hook Usage

Migration Example

Platform Support

Best Practices

  1. Use Prepared Statements: For repeated queries, use prepareAsync() for better performance
  2. Transactions: Use transactions for multiple related operations
  3. Close Connections: Always close database connections when done
  4. Parameterized Queries: Use ? placeholders to prevent SQL injection
  5. Error Handling: Wrap database operations in try/catch blocks
  6. Migrations: Implement version-based migrations for schema changes
Always use parameterized queries with ? placeholders instead of string concatenation to prevent SQL injection attacks.

Resources