← Plugins / SQLite Storage
This community plugin gives your app a real, persistent SQLite database with a familiar
transaction-based API. Unlike localStorage, the data is stored natively and isn't
cleared by the web view — ideal for structured records, offline caches and larger datasets.
cordova plugin add cordova-sqlite-storage
Open (or create) a database via window.sqlitePlugin after deviceready:
var db = window.sqlitePlugin.openDatabase({
name: "dedris.db",
location: "default"
});
| Method | Description |
|---|---|
sqlitePlugin.openDatabase(options) | Open or create a database and return a handle. |
db.transaction(fn, error, success) | Run a read/write transaction; rolls back automatically on error. |
db.executeSql(sql, params, success, error) | Run a single statement outside an explicit transaction. |
tx.executeSql(sql, params, success, error) | Run a statement inside a transaction; success receives a result set. |
db.close(success, error) | Close the database handle. |
Create a table, insert a row, and read it back:
document.addEventListener("deviceready", onReady, false);
function onReady() {
var db = window.sqlitePlugin.openDatabase({ name: "dedris.db", location: "default" });
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, body TEXT)");
tx.executeSql("INSERT INTO notes (body) VALUES (?)", ["Hello"]);
}, function (err) {
console.error("Tx error: " + err.message);
}, function () {
db.executeSql("SELECT * FROM notes", [], function (rs) {
console.log("Rows: " + rs.rows.length);
});
});
}
?) instead of string concatenation to avoid SQL injection.transaction so they commit or roll back together.localStorage API may be simpler than SQLite.