Monaca Docs
Search…
Cordova SQLite Storage Plugin
This Cordova/PhoneGap plugin uses SQLite databases on Android, iOS and Windows with an HTML5/Web SQL API.
To check the third party Cordova plugins, you need to create a custom build debugger (Android version or iOS version).

Demo

Enable the plugin in Monaca IDE

    1.
    From the IDE menu, go to Config → Manage Cordova Plugins .
    2.
    Click the Import Cordova Plugin button. Then, you can choose to import the plugin using a ZIP file or a URL/package name.

Usage

After importing the plugin into the project, you can initialize your database. Please make sure to call the plugin API after the Cordova is loaded.
1
var database = null;
2
3
document.addEventListener("deviceready", function(){
4
// Initialize the database after the Cordova is ready.
5
initDatabase();
6
});
7
8
function initDatabase() {
9
database = window.sqlitePlugin.openDatabase({name: 'sample.db', location: 'default'});
10
11
database.transaction(function(transaction) {
12
transaction.executeSql('CREATE TABLE SampleTable (name, score)');
13
});
14
}
Copied!

API references

This section describes some of the main functions used in the Demo. For complete API references, please refer to the repository.

Self-test functions

echoTest()

Verify that both the Javascript and native part of this plugin are installed in your application.
1
window.sqlitePlugin.echoTest(successCallback, errorCallback);
Copied!
Return Value
    Promise

selfTest()

Verify that this plugin is able to open a database, execute the CRUD (create, read, update, and delete) operations, and clean it up properly.
1
window.sqlitePlugin.selfTest(successCallback, errorCallback);
Copied!
Return Value
    Promise

Opening a database

Open a database access handle object.
1
window.sqlitePlugin.openDatabase({name: String, [location: String], [iosDatabaseLocation: String]}, [successCallback], [errorCallback]);
Copied!
Parameter
Name
Type
Description
name
String
Name of the database
location
String
[optional] Location of the database
iosDatabaseLocation
String
[optional] iOS Database Location. Available options are:
Return Value
    Promise
Example
An example of opening a database access handle object with a default or a specified location:
1
// use the default location
2
var database = window.sqlitePlugin.openDatabase({name: 'my.db', location: 'default'});
3
4
// use a specified location for iOS only
5
var database = window.sqlitePlugin.openDatabase({name: 'my.db', iosDatabaseLocation: 'Library'});
Copied!
The successcb and errorcb callback parameters are optional but can be extremely helpful in case anything goes wrong. For example:
1
window.sqlitePlugin.openDatabase({name: 'my.db', location: 'default'}, function(db) {
2
db.transaction(function(tx) {
3
// ...
4
}, function(err) {
5
console.log('Open database ERROR: ' + JSON.stringify(err));
6
});
7
});
Copied!

SQL transactions

Execute SQL on the opened database.
1
database.executeSql(SQL_STATEMENT: String, [] , [successCallback], [errorCallback]);
Copied!
Return Value
    Promise
Example
1
database.transaction(function(transaction) {
2
transaction.executeSql('SELECT count(*) AS recordCount FROM SampleTable', [], function(ignored, resultSet) {
3
$('#result').text('RECORD COUNT: ' + resultSet.rows.item(0).recordCount);
4
});
5
}, function(error) {
6
alert('SELECT count error: ' + error.message);
7
});
Copied!
1
database.transaction(function(transaction) {
2
transaction.executeSql('DELETE FROM SampleTable');
3
}, function(error) {
4
alert('DELETE error: ' + error.message);
5
}, function() {
6
alert('DELETE OK');
7
});
Copied!

Deleting a database

Delete a database access handle object.
1
window.sqlitePlugin.deleteDatabase({name: String, [location: String], [iosDatabaseLocation: String]}, [successCallback], [errorCallback]);
Copied!
Parameter
Name
Type
Description
name
String
Name of the database
location
String
[optional] Location of the database
iosDatabaseLocation
String
[optional] iOS Database Location. Available options are:
Return Value
    Promise
Example
1
// use the default location
2
window.sqlitePlugin.deleteDatabase({name: 'my.db', location: 'default'}, successcb, errorcb);
3
4
// use a specified location for iOS only
5
window.sqlitePlugin.deleteDatabase({name: 'my.db', iosDatabaseLocation: 'Library'});
Copied!
Last modified 2mo ago