@napacitor/database-sqlite
Local SQLite database plugin: create tables, CRUD operations, transactions, and database file management. Implemented in C# on top of Microsoft.Data.Sqlite, one codebase covering Android, iOS, Mac Catalyst, and Windows.
Installation
To use npm:
npm install @napacitor/database-sqlite
To use yarn:
yarn add @napacitor/database-sqlite
Sync native files:
npx nap sync
Usage
import { DatabaseSqlite } from '@napacitor/database-sqlite';
// Create a table (multiple statements run sequentially, separated by semicolons)
await DatabaseSqlite.execute({
statements:
'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);',
});
// Insert (placeholders ? or $1, $2 map to values in order, preventing SQL injection)
const { lastId } = await DatabaseSqlite.run({
statement: 'INSERT INTO users (name) VALUES (?);',
values: ['Max'],
});
// Query: each row comes back as a column name → value object
const result = await DatabaseSqlite.query({
statement: 'SELECT * FROM users WHERE name = ?;',
values: ['Max'],
});
console.log(result.values); // [{ id: 1, name: 'Max' }]
// Batch writes in a transaction (rolled back as a whole on failure)
await DatabaseSqlite.execute({
statements:
"INSERT INTO users (name) VALUES ('A'); INSERT INTO users (name) VALUES ('B');",
transaction: true,
});
// Database file management
const { exists } = await DatabaseSqlite.isDatabaseExists({
database: 'users.db3',
});
const list = await DatabaseSqlite.getDatabaseList(); // { values: ['napacitor.db3', ...] }
await DatabaseSqlite.deleteDatabase({ database: 'users.db3' });
// Connection management (connections are cached by database name; usually not needed)
await DatabaseSqlite.close({ database: 'users.db3' });
await DatabaseSqlite.closeAll();
How It Works
Wraps the SQLite approach described in the .NET MAUI local database documentation, using Microsoft.Data.Sqlite on the C# side:
- Database files live in the app data directory (
FileSystem.AppDataDirectory); - Connections are cached by database name; all SQL operations are serialized (SQLite connections are not thread-safe);
execute()→ one or more statements (semicolon-separated, sequential), optional transaction;run()→ a single statement with parameters, returningchangesand the last insertedlastId;query()→ a parameterized query, returning all rows (INTEGER → number, TEXT → string, BLOB → base64 string);- Value mapping: number → INTEGER/REAL, string → TEXT, boolean → 1/0, null → NULL;
- Database names allow plain file names only (no path traversal); the default database is
napacitor.db3.
If your app also uses sqlite-net-pcl on the C# side, both packages can coexist; each registers its own SQLitePCLRaw provider (bundle_e_sqlite3 / bundle_green), and the last registered one wins at runtime — generally no conflict.
Platform Notes
| Platform | Notes |
|---|---|
| Android / iOS / Mac Catalyst / Windows | All APIs available (the SQLitePCLRaw native library ships automatically via Microsoft.Data.Sqlite) |
| Web | Not supported. Browsers have no built-in SQLite; all methods reject. Use a WASM solution such as sql.js or wa-sqlite if you need browser-side SQLite |
Permissions
No configuration required (SQLite files live inside the app data directory and need no system permissions).
API
execute(options)
Executes one or more SQL statements that return no result sets (CREATE TABLE, INSERT/UPDATE/DELETE, DDL, etc.). Statements run sequentially; with transaction: true they commit as a whole and roll back on failure.
| Param | Type |
|---|---|
options | ExecuteOptions |
Returns: Promise<ExecuteResult>
run(options)
Executes a single parameterized statement (INSERT/UPDATE/DELETE), returning the number of affected rows and the last inserted rowid.
| Param | Type |
|---|---|
options | RunOptions |
Returns: Promise<RunResult>
query(options)
Executes a parameterized query and returns all result rows.
| Param | Type |
|---|---|
options | QueryOptions |
Returns: Promise<QueryResult>
close(options?)
Closes a database connection (it is reopened on next use).
| Param | Type |
|---|---|
options | CloseOptions |
Returns: Promise<void>
closeAll()
Closes all open database connections.
Returns: Promise<void>
deleteDatabase(options)
Deletes a database file (closing its connection first).
| Param | Type |
|---|---|
options | DeleteDatabaseOptions |
Returns: Promise<DeleteDatabaseResult>
isDatabaseExists(options)
Checks whether a database file exists.
| Param | Type |
|---|---|
options | IsDatabaseExistsOptions |
Returns: Promise<IsDatabaseExistsResult>
getDatabaseList()
Lists the SQLite database file names created in the app data directory.
Returns: Promise<GetDatabaseListResult>
Interfaces
ExecuteOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name; defaults to napacitor.db3. |
statements | string | One or more SQL statements (semicolon-separated; no parameters). |
transaction | boolean | Wrap multiple statements in a transaction (roll back on failure). |
ExecuteResult
| Prop | Type | Description |
|---|---|---|
changes | number | Number of affected rows (cumulative). |
RunOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name; defaults to napacitor.db3. |
statement | string | A single SQL statement (with parameter placeholders). |
values | unknown[] | Parameter values matching the placeholders in order (number/string/boolean/null). |
transaction | boolean | Wrap in a transaction (roll back on failure). |
RunResult
| Prop | Type | Description |
|---|---|---|
changes | number | Number of affected rows. |
lastId | number | The auto-increment rowid of the most recent INSERT (0 when no insert). |
QueryOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name; defaults to napacitor.db3. |
statement | string | Query SQL statement (with parameter placeholders). |
values | unknown[] | Parameter values matching the placeholders in order (number/string/boolean/null). |
QueryResult
| Prop | Type | Description |
|---|---|---|
values | Record<string, unknown>[] | Result rows; each row is a column name → value object (duplicate column names: last one wins). |
CloseOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name to close; defaults to napacitor.db3. |
DeleteDatabaseOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name to delete. |
DeleteDatabaseResult
| Prop | Type | Description |
|---|---|---|
deleted | boolean | Whether a file was actually deleted (false if missing). |
IsDatabaseExistsOptions
| Prop | Type | Description |
|---|---|---|
database | string | Database file name to check; defaults to napacitor.db3. |
IsDatabaseExistsResult
| Prop | Type | Description |
|---|---|---|
exists | boolean | Whether the database file exists. |
GetDatabaseListResult
| Prop | Type | Description |
|---|---|---|
values | string[] | SQLite database file names created in the app data directory. |