Phonegap-SQLitePlugin

A phonegap plugin to open and use sqlite databases on iOS.

  • 所有者: davibe/Phonegap-SQLitePlugin
  • 平台:
  • 许可证:
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

PGSqlitePlugin: SQLitePlugin for Phonegap

This plugin exists because I developed some large enterprise (boring) mobile
applications and found some problems using WebkitSQLite

The plugin is meant to work with Phonegap 1.7.0 (Cordova).

The API is not the same as HTML5 Web Sql one. I'm trying to move things in that
direction though.

Installing

SQLite library

In order to use the plugin you need to link the sqlite library in your phonegap application.

In your projects "Build Phases" tab, select the first "Link Binary with Libraries" dropdown menu and add the library libsqlite3.dylib or libsqlite3.0.dylib.

NOTE: In the "Build Phases" there can be multiple "Link Binary with Libraries" dropdown menus. Please select the first one otherwise it will not work.

PGSQLite Plugin

Drag .h and .m files into your project's Plugins folder (in xcode) -- I always
just have "Create references" as the option selected.

Take the precompiled javascript file from build/, or compile the coffeescript
file in src/ to javascript WITH the top-level function wrapper option (default).

Use the resulting javascript file in your HTML.

Look for the following to your project's PhoneGap.plist:

<key>Plugins</key>
<dict>
  ...
</dict>

Insert this in there:

<key>PGSQLitePlugin</key>
<string>PGSQLitePlugin</string>

General Usage

www/index.html contains a test application that runs very simple queries
using plain javascript. It is recommended that you first run this one and check
the XCode console to see that everything is working fine.

The following examples show you how you can use transactions.

Coffee Script

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 (tx) ->

  tx.executeSql [ "INSERT INTO test_table (data, data_num) VALUES (?,?)", "test", 100], (res) ->

    # success callback

    console.log "insertId: #{res.insertId} -- probably 1"
    console.log "rowsAffected: #{res.rowsAffected} -- should be 1"

    # 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"

  , (e) ->

    # error callback

    console.log "ERROR: #{e.message}"

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);
  });
});

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)

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)

kvstore = new Lawnchair { name: "kvstore", adapter: PGSQLitePlugin.lawnchair_adapter }, () ->
  # do stuff

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", ...}

Other notes from @Joenoon:

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.

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.

主要指标

概览
名称与所有者davibe/Phonegap-SQLitePlugin
主编程语言JavaScript
编程语言CoffeeScript (语言数: 4)
平台
许可证
所有者活动
创建于2011-06-23 05:49:47
推送于2012-05-05 18:05:33
最后一次提交2012-05-05 19:07:21
发布数1
最新版本名称wasabi (发布于 )
第一版名称wasabi (发布于 )
用户参与
星数190
关注者数23
派生数88
提交数41
已启用问题?
问题数24
打开的问题数12
拉请求数5
打开的拉请求数1
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?