Quick start
This page gets a database open, migrated, written to, and queried in a Capacitor app on any supported platform.
Prerequisites
- A Capacitor 8+ app (
@capacitor/coreand@capacitor/ios/@capacitor/androidas needed). - For Electron targets: Electron 40+ (bundles Node 24+, which ships
node:sqlite). - For Web targets: your dev/prod server must send cross-origin isolation headers — see Installation → Web.
Install
npm install @devioarts/capacitor-sqlite
npx cap sync
npx cap sync copies the iOS/Android native sources and (for Electron) wires up the plugin's
Electron entry points into your Capacitor project.
Check availability
isAvailable() reports whether SQLite can be used on the current platform. ':memory:'
databases still work even when it reports false (for example, Web without OPFS support).
import { CapacitorSqlite } from '@devioarts/capacitor-sqlite';
const availability = await CapacitorSqlite.isAvailable();
if (!availability.success || !availability.data.available) {
throw new Error(availability.success ? 'SQLite is not available' : availability.error.message);
}
Open a database and run a migration
open() reads PRAGMA user_version and runs every migration whose version exceeds the
stored value, in order, before resolving.
const opened = await CapacitorSqlite.open({
database: 'myapp',
migrations: [
{
version: 1,
statements: ['CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)'],
},
],
});
if (!opened.success) throw new Error(opened.error.message);
Write and read
Every method resolves to a SqliteResult — check success instead of relying on try/catch.
const inserted = await CapacitorSqlite.run({
database: 'myapp',
statement: 'INSERT INTO users (name) VALUES (?)',
values: ['Alice'],
});
if (!inserted.success) throw new Error(inserted.error.message);
console.log('Inserted row id:', inserted.data.lastInsertId);
const queried = await CapacitorSqlite.query<{ id: number; name: string }>({
database: 'myapp',
statement: 'SELECT * FROM users',
});
if (!queried.success) throw new Error(queried.error.message);
console.log('Rows:', queried.data.rows);
Close
await CapacitorSqlite.close({ database: 'myapp' });
Success signal
If queried.data.rows logs [{ id: 1, name: 'Alice' }], migrations, writes, and reads are all
working end to end on the current platform.
Next steps
- Platform-specific setup (Electron registration, Web headers): Installation.
- The
SqliteResultcontract, migration model, and transaction scoping: Concepts. - Bind values, placeholders, and BLOB handling: Usage.