Key-Value Storage Made Simple with the SQLite Plugin¶
Storing simple key-value data in Ionic and Capacitor apps often means choosing between unreliable browser storage or writing boilerplate SQL. The SQLite plugin now includes SqliteKeyValueStore — a built-in class that gives you a familiar get/set API backed by a real SQLite database. No SQL queries, no schema setup, no risk of data loss from WebView storage clearing. In this guide, you'll learn how to use SqliteKeyValueStore in your Capacitor apps and when to choose it over the Secure Preferences plugin.
What is SqliteKeyValueStore?¶
SqliteKeyValueStore is a built-in class shipped with the SQLite plugin that wraps a SQLite database behind a minimal key-value API. The database is automatically created and managed under the hood — you don't need to define schemas, write migrations, or execute any SQL. Values are stored as strings, so you can use JSON.stringify and JSON.parse to work with objects.
Here's what makes it a great choice for your next project:
- Reliable persistence — Data lives in a SQLite database file, not in WebView storage that the OS can clear at any time.
- No SQL required — A simple
get/setinterface. No schemas, no migrations, no queries. - Cross-platform — Works on Android, iOS, Web, and Electron with a single API.
- Performance — SQLite is optimized for fast reads and writes, even with larger data sets.
- Scales with your app — Start with key-value storage today, graduate to full SQL queries later using the same plugin — no migration needed.
For more details on the available methods, check out the Key-Value Store section in the plugin documentation.
SQLite Key Value Store vs. Secure Preferences¶
If you're already familiar with the Secure Preferences plugin, you might be wondering when to use which. Both provide key-value storage, but they're designed for different use cases:
| SQLite Key Value Store | Secure Preferences | |
|---|---|---|
| Best for | App settings, caching, larger data sets | Sensitive data (tokens, keys, secrets) |
| Encryption | Optional (requires SQLCipher setup) | Built-in, uses platform keychain/keystore |
| Third-party dependency | SQLCipher (only if encryption needed) | None |
| Performance with large data | Optimized (SQLite engine) | Designed for small values |
| SQL upgrade path | Yes, same plugin | No |
Use Secure Preferences for sensitive credentials that need encryption by default with zero setup. Use SQLite Key Value Store when you need reliable persistence for larger data sets with SQLite-level performance and an optional upgrade path to full SQL.
Getting Started¶
To get started with SqliteKeyValueStore, you first need to install the SQLite plugin. Please refer to the Installation section in the plugin documentation.
Usage¶
Once the plugin is installed, you can start using SqliteKeyValueStore right away. Let's walk through the most common operations.
Storing Data¶
First, create a SqliteKeyValueStore instance. Then use set(...) to store simple strings or JSON-serialized objects:
import { Sqlite, SqliteKeyValueStore } from '@capawesome-team/capacitor-sqlite';
const store = new SqliteKeyValueStore(Sqlite);
// Store a simple string value
await store.set({ key: 'language', value: 'en' });
// Store an object as a JSON string
const preferences = { theme: 'dark', fontSize: 16, notifications: true };
await store.set({ key: 'preferences', value: JSON.stringify(preferences) });
Retrieving Data¶
Use get(...) to read a value by its key. The returned value is null if the key doesn't exist, so always check before parsing:
const { value } = await store.get({ key: 'preferences' });
if (value) {
const preferences = JSON.parse(value);
console.log(preferences.theme); // 'dark'
}
Removing Data¶
Use remove(...) to delete a single key or clear() to wipe all stored data:
Listing All Keys¶
Use keys() to get a list of all stored keys:
const { keys } = await store.keys();
console.log(keys); // ['preferences', 'onboardingComplete', ...]
Use Cases¶
Here are some common scenarios where SqliteKeyValueStore is a great fit:
- User preferences — Theme, language, font size, or notification settings. Data persists reliably across app restarts without worrying about WebView storage limits.
- Feature flags and onboarding state — Track which features are enabled or whether the user has completed onboarding. Simple boolean or string values that need to survive app updates.
- Lightweight caching — Cache API responses or computed results as JSON strings. SQLite performance keeps reads fast even as your cache grows.
Conclusion¶
SqliteKeyValueStore gives you reliable, performant key-value storage with zero SQL boilerplate — and a clear path to full SQL when your app needs it. If you're looking for a simple yet robust way to persist data in your Capacitor app, give it a try. For more details, check out the full SQLite plugin documentation and the Key-Value Store API reference.
If you have any questions or need help, join the Capawesome Discord server to connect with the community. To stay updated with the latest news about Capawesome, Capacitor, and the Ionic ecosystem, subscribe to the Capawesome newsletter.