Skip to main content

@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, returning changes and the last inserted lastId;
  • 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​

PlatformNotes
Android / iOS / Mac Catalyst / WindowsAll APIs available (the SQLitePCLRaw native library ships automatically via Microsoft.Data.Sqlite)
WebNot 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.

ParamType
optionsExecuteOptions

Returns: Promise<ExecuteResult>

run(options)​

Executes a single parameterized statement (INSERT/UPDATE/DELETE), returning the number of affected rows and the last inserted rowid.

ParamType
optionsRunOptions

Returns: Promise<RunResult>

query(options)​

Executes a parameterized query and returns all result rows.

ParamType
optionsQueryOptions

Returns: Promise<QueryResult>

close(options?)​

Closes a database connection (it is reopened on next use).

ParamType
optionsCloseOptions

Returns: Promise<void>

closeAll()​

Closes all open database connections.

Returns: Promise<void>

deleteDatabase(options)​

Deletes a database file (closing its connection first).

ParamType
optionsDeleteDatabaseOptions

Returns: Promise<DeleteDatabaseResult>

isDatabaseExists(options)​

Checks whether a database file exists.

ParamType
optionsIsDatabaseExistsOptions

Returns: Promise<IsDatabaseExistsResult>

getDatabaseList()​

Lists the SQLite database file names created in the app data directory.

Returns: Promise<GetDatabaseListResult>

Interfaces​

ExecuteOptions​

PropTypeDescription
databasestringDatabase file name; defaults to napacitor.db3.
statementsstringOne or more SQL statements (semicolon-separated; no parameters).
transactionbooleanWrap multiple statements in a transaction (roll back on failure).

ExecuteResult​

PropTypeDescription
changesnumberNumber of affected rows (cumulative).

RunOptions​

PropTypeDescription
databasestringDatabase file name; defaults to napacitor.db3.
statementstringA single SQL statement (with parameter placeholders).
valuesunknown[]Parameter values matching the placeholders in order (number/string/boolean/null).
transactionbooleanWrap in a transaction (roll back on failure).

RunResult​

PropTypeDescription
changesnumberNumber of affected rows.
lastIdnumberThe auto-increment rowid of the most recent INSERT (0 when no insert).

QueryOptions​

PropTypeDescription
databasestringDatabase file name; defaults to napacitor.db3.
statementstringQuery SQL statement (with parameter placeholders).
valuesunknown[]Parameter values matching the placeholders in order (number/string/boolean/null).

QueryResult​

PropTypeDescription
valuesRecord<string, unknown>[]Result rows; each row is a column name → value object (duplicate column names: last one wins).

CloseOptions​

PropTypeDescription
databasestringDatabase file name to close; defaults to napacitor.db3.

DeleteDatabaseOptions​

PropTypeDescription
databasestringDatabase file name to delete.

DeleteDatabaseResult​

PropTypeDescription
deletedbooleanWhether a file was actually deleted (false if missing).

IsDatabaseExistsOptions​

PropTypeDescription
databasestringDatabase file name to check; defaults to napacitor.db3.

IsDatabaseExistsResult​

PropTypeDescription
existsbooleanWhether the database file exists.

GetDatabaseListResult​

PropTypeDescription
valuesstring[]SQLite database file names created in the app data directory.