2012-12-04 17:26:25 -05:00
# Cordova/PhoneGap sqlitePlugin - Android version
2012-04-01 16:50:16 -04:00
2012-12-04 17:26:25 -05:00
Native interface to sqlite in a Cordova/PhoneGap plugin, working to follow the HTML5 Web SQL API as close as possible.
2011-06-23 10:03:47 -04:00
2012-12-01 15:18:20 -05:00
Extracted from DroidGap by @brodyspark (Chris Brody)
2012-03-27 02:31:55 -04:00
2012-12-01 15:18:20 -05:00
Nested transaction callback support by @marcucio
2012-03-27 02:31:55 -04:00
2012-12-06 16:26:30 -05:00
License for this version: MIT or Apache
2012-08-05 16:21:30 -04:00
## Announcements
2012-12-04 18:57:54 -05:00
- [Improvements in the form of PRAGMAs & multiple database files (bug fix) ](http://brodyspark.blogspot.com/2012/12/improvements-to-phonegap-sqliteplugin.html ).
2012-12-04 17:20:38 -05:00
- The Android version is now maintained in this location as [announced here ](http://brodyspark.blogspot.com/2012/12/phonegap-sqliteplugin-for-ios-android.html ).
2012-12-01 15:18:20 -05:00
2012-07-24 13:19:59 -04:00
## Highlights
2011-12-02 10:45:09 -05:00
2012-12-01 15:18:20 -05:00
- Keeps sqlite database in a user data location that is known and can be reconfigured
2012-04-13 08:23:03 -04:00
- Drop-in replacement for HTML5 SQL API, the only change is window.openDatabase() --> sqlitePlugin.openDatabase()
2012-12-01 15:18:20 -05:00
- batch processing optimizations
2012-12-06 16:26:30 -05:00
- No 5MB maximum, more information at: http://www.sqlite.org/limits.html
2011-09-26 19:58:05 -04:00
2012-12-03 17:13:34 -05:00
This sqlitePlugin can also be used with SQLCipher to provide encryption. This was already described on my old blog:
- [Android version with rebuilding SQLCipher from source ](http://mobileapphelp.blogspot.com/2012/08/rebuilding-sqlitesqlcipher-for-android.html )
- [Android version tested with SQLCipher for database encryption ](http://mobileapphelp.blogspot.com/2012/08/trying-sqlcipher-with-cordova.html ), working with a few changes to SQLitePlugin.java
2012-12-04 17:19:02 -05:00
Updated instructions will be posted on my [new blog ](http://brodyspark.blogspot.com/ ) sometime in the near future.
2012-12-03 17:13:34 -05:00
2012-12-04 17:26:25 -05:00
## Apps using Cordova/PhoneGap sqlitePlugin (Android version)
2012-08-06 13:41:33 -04:00
2012-12-01 15:18:20 -05:00
- [Get It Done app ](http://getitdoneapp.com/ ) by [marcucio.com ](http://marcucio.com/ )
2012-12-04 17:19:02 -05:00
- Upcoming (under development): Arbiter disastery recovery app by [LMN Solutions ](http://lmnsolutions.com/ )
2012-08-06 13:41:33 -04:00
I would like to gather some more real-world examples, please send to chris.brody@gmail.com and I will post them.
2012-07-24 13:19:59 -04:00
## Known limitations
2012-12-01 15:18:20 -05:00
- Versioning functionality is missing
2012-07-24 13:19:59 -04:00
- API will block app execution upon large batching (workaround: add application logic to break large batches into smaller batch transactions)
2012-12-01 15:18:20 -05:00
- `rowsAffected` field in the response to UPDATE and DELETE is not working
2012-07-24 13:19:59 -04:00
2012-04-12 08:17:59 -04:00
Usage
=====
The idea is to emulate the HTML5 SQL API as closely as possible. The only major change is to use window.sqlitePlugin.openDatabase() (or sqlitePlugin.openDatabase()) instead of window.openDatabase(). If you see any other major change please report it, it is probably a bug.
2012-12-03 17:13:34 -05:00
# Sample with PRAGMA feature
2012-04-21 13:30:46 -04:00
This is a pretty strong test: first we create a table and add a single entry, then query the count to check if the item was inserted as expected. Note that a new transaction is created in the middle of the first callback.
2012-04-12 08:17:59 -04:00
2012-04-21 09:43:36 -04:00
// Wait for Cordova to load
2012-04-12 08:17:59 -04:00
//
document.addEventListener("deviceready", onDeviceReady, false);
2012-04-21 09:43:36 -04:00
// Cordova is ready
2012-04-12 08:17:59 -04:00
//
function onDeviceReady() {
2012-12-06 15:18:46 -05:00
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
2012-04-12 08:17:59 -04:00
2012-04-21 09:43:36 -04:00
db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
2012-04-12 08:17:59 -04:00
2012-12-04 18:57:54 -05:00
// demonstrate PRAGMA:
2012-12-03 17:13:34 -05:00
db.executePragmaStatement("pragma table_info (test_table);", function(res) {
alert("PRAGMA res: " + JSON.stringify(res));
});
2012-04-21 13:30:46 -04:00
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
console.log("insertId: " + res.insertId + " -- probably 1");
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
2012-12-03 17:13:34 -05:00
alert("insertId: " + res.insertId + " -- probably 1");
2012-04-21 13:30:46 -04:00
db.transaction(function(tx) {
tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
2012-12-03 17:13:34 -05:00
alert("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
2012-04-21 13:30:46 -04:00
});
});
}, function(e) {
console.log("ERROR: " + e.message);
});
});
}
## Sample with transaction-level nesting
2012-12-01 15:18:20 -05:00
In this case, the same transaction in the first executeSql() callback is being reused to run executeSql() again.
2012-04-21 13:30:46 -04:00
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
2012-12-06 15:18:46 -05:00
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
2012-04-21 13:30:46 -04:00
db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
2012-04-21 09:43:36 -04:00
console.log("insertId: " + res.insertId + " -- probably 1");
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
2012-04-12 08:17:59 -04:00
2012-04-21 09:43:36 -04:00
tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
2012-04-21 13:30:46 -04:00
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
2012-04-21 09:43:36 -04:00
});
2012-04-12 08:17:59 -04:00
2012-04-21 09:43:36 -04:00
}, function(e) {
2012-04-21 13:30:46 -04:00
console.log("ERROR: " + e.message);
2012-04-12 08:17:59 -04:00
});
});
}
2012-04-21 13:30:46 -04:00
This case will also works with Safari (WebKit), assuming you replace window.sqlitePlugin.openDatabase with window.openDatabase.
2012-12-01 15:18:20 -05:00
# Installing
2012-03-07 10:50:27 -05:00
2012-04-11 14:51:14 -04:00
**NOTE:** There are now the following trees:
- `Android` : new version by @marcucio , with improvements for batch transaction processing, testing seems OK
2012-05-05 12:07:15 -04:00
- `test-www` : simple testing in `index.html` using qunit 1.5.0
2012-12-06 16:26:30 -05:00
- `xtra-DroidGap-test` : old DroidGap version, no improvements for batch processing, simple version to test some fixes before adding to `Android` version (going away)
2012-04-01 16:10:22 -04:00
2012-07-23 11:34:45 -04:00
## Android
2012-12-04 17:26:25 -05:00
These installation instructions are based on the Android example project from Cordova/PhoneGap 2.2.0. For your first time please unzip the PhoneGap 2.2 zipball and use the `lib/android/example` subdirectory.
2012-07-23 11:34:45 -04:00
- Install Android/assets/www/SQLitePlugin.js from this repository into assets/www subdirectory
- Install Android/src/com/phonegap/plugin/sqlitePlugin/SQLitePlugin.java from this repository into src/com/phonegap/plugin/sqlitePlugin subdirectory
2012-07-24 13:49:34 -04:00
- Add the plugin element < plugin name = "SQLitePlugin" value = "com.phonegap.plugin.sqlitePlugin.SQLitePlugin" /> to res/xml/config.xml for Cordova 2.0+ (res/xml/plugins.xml for Cordova pre-2.0)
2012-07-23 11:34:45 -04:00
2012-07-24 13:49:34 -04:00
Sample change to res/xml/config.xml:
2012-07-23 11:34:45 -04:00
2012-07-24 13:49:34 -04:00
--- config.xml.old 2012-07-24 19:44:49.000000000 +0200
+++ res/xml/config.xml 2012-07-24 19:39:43.000000000 +0200
@@ -32,6 +32,7 @@
< log level = "DEBUG" / >
< preference name = "useBrowserHistory" value = "false" / >
2012-07-23 11:34:45 -04:00
< plugins >
2012-07-23 16:52:13 -04:00
+ < plugin name = "SQLitePlugin" value = "com.phonegap.plugin.sqlitePlugin.SQLitePlugin" / >
2012-07-23 11:34:45 -04:00
< plugin name = "App" value = "org.apache.cordova.App" / >
< plugin name = "Geolocation" value = "org.apache.cordova.GeoBroker" / >
< plugin name = "Device" value = "org.apache.cordova.Device" / >
2012-08-04 09:47:59 -04:00
Before building for the first time, you have to update the project with the desired version of the Android SDK with a command like:
android update project --path $(pwd) --target 15
(assume SDK 15, use the correct desired Android SDK number here)
2012-07-23 11:34:45 -04:00
### Quick test
2012-12-04 18:57:54 -05:00
Make a change like this to index.html (or use the sample code) verify proper installation:
2012-07-23 16:52:13 -04:00
2012-08-04 08:43:17 -04:00
--- index.html.old 2012-08-04 14:40:07.000000000 +0200
+++ assets/www/index.html 2012-08-04 14:36:05.000000000 +0200
@@ -24,7 +24,35 @@
2012-07-23 16:52:13 -04:00
< title > PhoneGap< / title >
< link rel = "stylesheet" href = "master.css" type = "text/css" media = "screen" title = "no title" >
2012-07-24 13:49:34 -04:00
< script type = "text/javascript" charset = "utf-8" src = "cordova-2.0.0.js" > < / script >
2012-07-23 16:52:13 -04:00
- < script type = "text/javascript" charset = "utf-8" src = "main.js" ></ script >
+ < script type = "text/javascript" charset = "utf-8" src = "SQLitePlugin.js" > < / script >
+
2012-08-04 08:43:17 -04:00
+
2012-07-23 16:52:13 -04:00
+ < script type = "text/javascript" charset = "utf-8" >
+ document.addEventListener("deviceready", onDeviceReady, false);
+ function onDeviceReady() {
2012-12-06 15:18:46 -05:00
+ var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
2012-07-23 16:52:13 -04:00
+
+ db.transaction(function(tx) {
+ tx.executeSql('DROP TABLE IF EXISTS test_table');
+ tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
+
+ tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
2012-08-04 09:10:10 -04:00
+ console.log("insertId: " + res.insertId + " -- probably 1"); // check #18/#38 is fixed
+ alert("insertId: " + res.insertId + " -- should be valid");
2012-07-23 16:52:13 -04:00
+
2012-08-04 08:43:17 -04:00
+ db.transaction(function(tx) {
2012-07-23 16:52:13 -04:00
+ tx.executeSql("SELECT data_num from test_table;", [], function(tx, res) {
+ console.log("res.rows.length: " + res.rows.length + " -- should be 1");
+ alert("res.rows.item(0).data_num: " + res.rows.item(0).data_num + " -- should be 100");
+ });
+ });
+
+ }, function(e) {
+ console.log("ERROR: " + e.message);
+ });
+ });
+ }
+ < / script >
< / head >
< body onload = "init();" id = "stage" class = "theme" >
2012-07-23 11:34:45 -04:00
2012-05-05 12:07:15 -04:00
# Unit test(s)
2012-12-06 16:26:30 -05:00
Unit testing is done in `test-www/index.html` . To run the test(s) yourself please copy the files from `test-www` (`index.html`, `qunit-1.5.0.js` , & `qunit-1.5.0.css` ) into the `www` directory of your Android Cordova project and make sure you have SQLitePlugin completely installed (JS, Java, and plugin registered).
2012-05-05 12:07:15 -04:00
2012-08-08 16:13:08 -04:00
# Loading pre-populated database file
2012-12-04 18:57:54 -05:00
[Excellent directions for the Android version ](http://www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap ) have been posted recently, directions needed for iOS version. [General directions for Cordova/PhoneGap ](http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html ) had been posted but seems out-of-date and does not specifically apply for this plugin.
2012-04-01 16:50:16 -04:00
2012-12-06 16:26:30 -05:00
# Support
If you have an issue with the plugin the best way to get help is by raising an issue. It is best to make post the simplest code necessary to demonstrate the issue.
2012-12-01 15:18:20 -05:00
# Extra notes from original iOS version
2012-04-11 14:51:14 -04:00
2012-12-01 15:18:20 -05:00
### Other notes from @Joenoon:
2011-09-28 13:42:22 -04:00
I played with the idea of batching responses into larger sets of
writeJavascript on a timer, however there was only a barely noticeable
performance gain. So I took it out, not worth it. However there is a
massive performance gain by batching on the client-side to minimize
PhoneGap.exec calls using the transaction support.
2012-07-29 18:35:36 -04:00
### Other notes from @davibe:
I used the plugin to store very large documents (1 or 2 Mb each) and found
that the main bottleneck was passing data from javascript to native code.
Running PhoneGap.exec took some seconds while completely blocking my
application.
2012-12-06 16:26:30 -05:00
# Contributing
- Testimonials of apps that are using this plugin would be especially helpful.
- Issue reports can help improve the quality of this plugin.
- Patches with bug fixes are helpful, especially when submitted with test code.
- Other enhancements will be considered if they do not increase the complexity of this plugin.