2011-06-23 10:03:47 -04:00
|
|
|
Phonegap SQLitePlugin
|
|
|
|
=====================
|
|
|
|
|
2011-12-02 10:45:09 -05:00
|
|
|
DISCLAIMER:
|
|
|
|
|
2011-09-28 13:42:22 -04:00
|
|
|
We are brand new to objective-c, so there could be problems with our code!
|
2011-09-26 19:58:05 -04:00
|
|
|
|
|
|
|
Installing
|
|
|
|
==========
|
2012-01-31 21:54:54 -05:00
|
|
|
/**
|
|
|
|
For installing with PhoneGap 1.3.0:
|
|
|
|
in PGSQLitePlugin.h file change for PhoneGaps JSONKit.h implementation.
|
|
|
|
|
|
|
|
[code]
|
|
|
|
#ifdef PHONEGAP_FRAMEWORK
|
|
|
|
#import <PhoneGap/PGPlugin.h>
|
|
|
|
#import <PhoneGap/JSONKit.h>
|
|
|
|
#import <PhoneGap/PhoneGapDelegate.h>
|
|
|
|
#import <PhoneGap/File.h>
|
|
|
|
#import<PhoneGap/FileTransfer.h>
|
|
|
|
#else
|
|
|
|
#import "PGPlugin.h"
|
|
|
|
#import "JSON.h"
|
|
|
|
#import "PhoneGapDelegate.h"
|
|
|
|
#import "File.h"
|
|
|
|
#endif
|
|
|
|
[/code]
|
|
|
|
|
|
|
|
In the Project Build Phases tab, select the "Link Binary with Libraries" dropdown menu and add 2 libraries:
|
|
|
|
|
2012-01-31 21:56:03 -05:00
|
|
|
libsqlite3.0.dylib
|
|
|
|
libsqlite3.dylib
|
|
|
|
|
|
|
|
|
2011-09-26 19:58:05 -04:00
|
|
|
Drag .h and .m files into your project's Plugins folder (in xcode) -- I always
|
|
|
|
just have "Create references" as the option selected.
|
|
|
|
|
2011-09-26 20:44:37 -04:00
|
|
|
Take the precompiled javascript file from build/, or compile the coffeescript
|
|
|
|
file in src/ to javascript WITH the top-level function wrapper option (default).
|
2011-09-26 19:58:05 -04:00
|
|
|
|
|
|
|
Use the resulting javascript file in your HTML.
|
|
|
|
|
|
|
|
Look for the following to your project's PhoneGap.plist:
|
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
<key>Plugins</key>
|
|
|
|
<dict>
|
|
|
|
...
|
|
|
|
</dict>
|
2011-09-26 19:58:05 -04:00
|
|
|
|
|
|
|
Insert this in there:
|
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
<key>PGSQLitePlugin</key>
|
|
|
|
<string>PGSQLitePlugin</string>
|
2011-09-26 19:58:05 -04:00
|
|
|
|
|
|
|
General Usage
|
|
|
|
=============
|
|
|
|
|
2011-09-28 13:42:22 -04:00
|
|
|
## Coffee Script
|
2011-09-26 19:58:05 -04:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
db = new PGSQLitePlugin("test_native.sqlite3")
|
|
|
|
db.executeSql('DROP TABLE IF EXISTS test_table')
|
|
|
|
db.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)')
|
2011-09-26 19:58:05 -04:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
db.transaction (tx) ->
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
tx.executeSql [ "INSERT INTO test_table (data, data_num) VALUES (?,?)", "test", 100], (res) ->
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
# success callback
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
console.log "insertId: #{res.insertId} -- probably 1"
|
|
|
|
console.log "rowsAffected: #{res.rowsAffected} -- should be 1"
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
# check the count (not a part of the transaction)
|
|
|
|
db.executeSql "select count(id) as cnt from test_table;", (res) ->
|
|
|
|
console.log "rows.length: #{res.rows.length} -- should be 1"
|
|
|
|
console.log "rows[0].cnt: #{res.rows[0].cnt} -- should be 1"
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
, (e) ->
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
# error callback
|
2011-12-02 10:45:09 -05:00
|
|
|
|
2011-09-26 20:05:16 -04:00
|
|
|
console.log "ERROR: #{e.message}"
|
2011-09-27 08:39:37 -04:00
|
|
|
|
2011-09-28 13:42:22 -04:00
|
|
|
## Plain Javascript
|
|
|
|
|
|
|
|
var db;
|
|
|
|
db = new PGSQLitePlugin("test_native.sqlite3");
|
|
|
|
db.executeSql('DROP TABLE IF EXISTS test_table');
|
|
|
|
db.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
|
|
|
|
db.transaction(function(tx) {
|
|
|
|
return tx.executeSql(["INSERT INTO test_table (data, data_num) VALUES (?,?)", "test", 100], function(res) {
|
|
|
|
console.log("insertId: " + res.insertId + " -- probably 1");
|
|
|
|
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
|
|
|
|
return db.executeSql("select count(id) as cnt from test_table;", function(res) {
|
|
|
|
console.log("rows.length: " + res.rows.length + " -- should be 1");
|
|
|
|
return console.log("rows[0].cnt: " + res.rows[0].cnt + " -- should be 1");
|
|
|
|
});
|
|
|
|
}, function(e) {
|
|
|
|
return console.log("ERROR: " + e.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2011-09-27 08:39:37 -04:00
|
|
|
Lawnchair Adapter Usage
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Include the following js files in your html:
|
|
|
|
|
|
|
|
- lawnchair.js (you provide)
|
|
|
|
- pgsqlite_plugin.js
|
|
|
|
- lawnchair_pgsqlite_plugin_adapter.js (must come after pgsqlite_plugin.js)
|
|
|
|
|
2011-12-02 10:45:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
The `name` option will determine the sqlite filename. Optionally, you can change it using the `db` option.
|
|
|
|
|
|
|
|
In this example, you would be using/creating the database at: *Documents/kvstore.sqlite3* (all db's in PGSQLitePlugin are in the Documents folder)
|
2011-09-27 08:39:37 -04:00
|
|
|
|
|
|
|
kvstore = new Lawnchair { name: "kvstore", adapter: PGSQLitePlugin.lawnchair_adapter }, () ->
|
|
|
|
# do stuff
|
2011-09-28 13:42:22 -04:00
|
|
|
|
2011-12-02 10:45:09 -05:00
|
|
|
Using the `db` option you can create multiple stores in one sqlite file. (There will be one table per store.)
|
|
|
|
|
|
|
|
recipes = new Lawnchair {db: "cookbook", name: "recipes", ...}
|
|
|
|
ingredients = new Lawnchair {db: "cookbook", name: "ingredients", ...}
|
2011-09-28 13:42:22 -04:00
|
|
|
|
2011-09-28 14:20:54 -04: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.
|
|
|
|
|