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 nameoptions(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
- Use Prepared Statements: For repeated queries, use
prepareAsync()for better performance - Transactions: Use transactions for multiple related operations
- Close Connections: Always close database connections when done
- Parameterized Queries: Use
?placeholders to prevent SQL injection - Error Handling: Wrap database operations in try/catch blocks
- Migrations: Implement version-based migrations for schema changes