• Stars
    star
    192
  • Rank 202,019 (Top 4 %)
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated over 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

next.js-example-with-global-stylesheet

Next.js example for including a global stylesheet with HMR
JavaScript
91
star
2

babel-plugin-wrap-in-js

Babel plugin transforming any file to a module exporting the file content as string
JavaScript
24
star
3

gitlist-docker

A ready to use docker image with preinstalled gitlist.
Nginx
21
star
4

webproducer

A flash player based RTMP encoder featuring with adobe FMLE style authentication and Javascript interface
JavaScript
17
star
5

cordova-webrtc-janus-gateway

An example of how to use PhoneRTC with janus-gateway
JavaScript
15
star
6

castoro

A tool to stream local files to chromecast. Device discovery, volume control, seeking support, automatic subtitles loading based on media filename, audio transcoding.
CoffeeScript
7
star
7

push-notification-service

A (simplistic, coffeescript, docker, rabbitmq, amqp, promise)-based push notification micrservice for ios and android applications.
CoffeeScript
6
star
8

osximagesrc

A gstreamer source plugin that captures the MacOSX screen
C
5
star
9

docka

A proof-of-concept OSX Dock alternative or completentary utility written in Swift
Swift
3
star
10

language-markdown-in-js

Atom markdown highlighting for markdown-in-js
JavaScript
3
star
11

gst-plugins-good

GStreamer plugins good
C
3
star
12

mmd-template

Some css styles I use to format most of my multimarkdown documents
JavaScript
3
star
13

next.js-example-with-markdown-in-js

This example shows how you can use `markdown-in-js` in a next.js application.
JavaScript
3
star
14

PhoneGap-Downloader

Downloader plugin for iOS PhoneGap that exposes the same API of its android counterpart (https://github.com/phonegap/phonegap-plugins/tree/master/Android/Downloader)
JavaScript
3
star
15

minimalgap

2
star
16

node-msp

Simple Multiwii Serial Protocol (MSP) implementation for node.js
JavaScript
2
star
17

ReactNativeExample

react+refnux+react-native-maps esample
JavaScript
2
star
18

node-tunnel

nodejs lib and utils to create simple TCP tunnels
JavaScript
2
star
19

chef-bootstrap

Chef bootstrapping scripts
Shell
2
star
20

gstreamer-docker

A docker image based on latest ubuntu release with prebuilt upstream version of gstreamer
Shell
1
star
21

async-js-coffee

CoffeeScript
1
star
22

chromecast-caf-sender

A sender app used to test davibe/chromecst-caf-receiver
Swift
1
star
23

lambda-s3-image-handler

A node-based package that implements an Amazon Lambda handler for scaling images on s3
CoffeeScript
1
star
24

twango

A script I use to launch Django web apps from twisted using Django's wsgi -handler
Python
1
star
25

gstdashplayer

gstreamer based dash player with JNI java interface
Shell
1
star
26

caccuino

render markdown documents with support for codeblock syntax hilight and mermaidjs diagrams
CSS
1
star
27

resiza

Simple dock-icon-app to quickly resize current window to large, medium, small
Swift
1
star
28

docker-gstreamer-raspbian-build

Building latest git version of gstreamer and plugins for raspberry pi (raspbian)
Dockerfile
1
star
29

baseflight-configurator

Google chrome/chromium based configurator
JavaScript
1
star
30

stream-swift

simple synchronous reactive event stream drop-in lib in swift
Swift
1
star