Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uk.co.workingedge.cordova.plugin.sqliteporter

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uk.co.workingedge.cordova.plugin.sqliteporter

Enables importing/exporting of SQLite databases to/from JSON/SQL.

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

SQLite Porter Cordova/Phonegap Plugin

Contents

  • Overview
  • Installation
  • Usage
  • JSON import optimisations
  • Example projects
  • License

Overview

This Cordova/Phonegap plugin can be used to import/export to/from a SQLite database using either SQL or JSON.

  • Works on all Cordova platforms that support HTML5 WebSQL DB in the WebView:
    • Android
    • iOS
    • Tizen
    • Blackberry 10
  • Works for native SQLite DB via a plugin. This can be used for platforms that don't have WebSQL in WebView (e.g. Windows) or for unlimited storage on platforms that do:
    • Android
    • iOS
    • Amazon Fire-OS
    • Windows Universal (8.1)
    • Windows Phone 7
    • Windows Phone 8
  • Import/export either just the table data or the entire table structure as well.

Usage scenarios

  • Create and populate a database from a database dump.
  • Update an existing database by importing inserts/updates/deletes.
  • Export and send data from a database.

Installation

Using the Cordova/Phonegap CLI

$ cordova plugin add uk.co.workingedge.cordova.plugin.sqliteporter
$ phonegap plugin add uk.co.workingedge.cordova.plugin.sqliteporter

Using Cordova Plugman

$ plugman install --plugin=uk.co.workingedge.cordova.plugin.sqliteporter --platform=<platform> --project=<project_path> --plugins_dir=plugins

For example, to install for the Android platform

$ plugman install --plugin=uk.co.workingedge.cordova.plugin.sqliteporter --platform=android --project=platforms/android --plugins_dir=plugins

PhoneGap Build

Add the following xml to your config.xml to use the latest version of this plugin from plugins.cordova.io:

<gap:plugin name="uk.co.workingedge.cordova.plugin.sqliteporter" source="plugins.cordova.io" />

or from npmjs.com:

<gap:plugin name="uk.co.workingedge.cordova.plugin.sqliteporter" source="npm" />

Usage

The plugin is exposed via the cordova.plugins.sqlitePorter object and provides the following functions:

importSqlToDb()

Executes a set of SQL statements against the defined database. Can be used to import data defined in the SQL statements into the database, and may additionally include commands to create the table structure.

cordova.plugins.sqlitePorter.importSqlToDb(db, sql, opts);

Parameters

  • {Database} db - open SQLite database to import into
  • {string} sql - SQL statements to execute against database.
  • {object} opts - optional parameters:
    • {function} successFn - callback function to execute once import is complete, called with arguments:
      • {integer} count - total number of statements executed in the given SQL string.
    • {function} errorFn - callback function to execute on error during import, called with arguments:
      • {object} error - object representing the error.
    • {function} progressFn - callback function to execute after each successful execution of SQL statement, called with arguments:
      • {object} count - number of statements executed so far.
      • {integer} totalCount - total number of statements in the given SQL string.

Example usage

Create a database from a SQL dump

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var sql = "CREATE TABLE Artist ([Id] PRIMARY KEY, [Title]);"+
    "INSERT INTO Artist(Id,Title) VALUES ('1','Fred');";
var successFn = function(count){
    alert("Successfully imported "+count+" SQL statements to DB");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Imported "+current+"/"+total+" statements";
};
cordova.plugins.sqlitePorter.importSqlToDb(db, sql, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn
});

Update an existing database

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var sql = "INSERT INTO Artist(Id,Title) VALUES ('6','Jane');"+
    "UPDATE Artist SET Title='Susan' WHERE Id='2';"+
    "DELETE FROM Artist WHERE Id='5';";
var successFn = function(count){
    alert("Successfully imported "+count+" SQL statements to DB");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Imported "+current+"/"+total+" statements";
};
cordova.plugins.sqlitePorter.importSqlToDb(db, sql, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn
});

exportDbToSql()

Exports a SQLite DB as a set of SQL statements.

cordova.plugins.sqlitePorter.exportDbToSql(db, opts);

Parameters

  • {Database} db - open SQLite database to export
  • {object} opts - optional parameters:
    • {function} successFn - callback function to execute after export is complete, with arguments:
      • {string} sql - exported SQL statements combined into a single string.
      • {integer} count - number of SQL statements in exported string.
    • {boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false.

Example usage

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var successFn = function(sql, count){
    console.log("Exported SQL: "+sql);
    alert("Exported SQL contains "+count+" statements");
};
cordova.plugins.sqlitePorter.exportDbToSql(db, {
    successFn: successFn
});

importJsonToDb()

Converts table structure and/or row data contained within a JSON structure into SQL statements that can be executed against a SQLite database. Can be used to import data into the database and/or create the table structure.

cordova.plugins.sqlitePorter.importJsonToDb(db, json, opts);

Parameters

  • {Database} db - open SQLite database to import into
  • {string/object} json - JSON structure containing row data and/or table structure as either a JSON object or string
  • {object} opts - optional parameters:
    • {function} successFn - callback function to execute once import is complete, called with arguments:
      • {integer} count - total number of statements executed in the given SQL string.
    • {function} errorFn - callback function to execute on error during import, called with arguments:
      • {object} error - object representing the error.
    • {function} progressFn - callback function to execute after each successful execution of SQL statement, called with arguments:
      • {object} count - number of statements executed so far.
      • {integer} totalCount - total number of statements in the given SQL string.

Example usage

Create a database from a SQL dump

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var json = {
    "structure":{
        "tables":{
            "Artist":"([Id] PRIMARY KEY, [Title])"
        },
       "otherSQL": [
            "CREATE UNIQUE INDEX Artist_ID ON Artist(Id)"
       ]
    },
    "data":{
        "inserts":{
            "Artist":[
                {
                    "Id":"1",
                    "Title":"Fred"
                }
            ]
        }
    }
};
var successFn = function(count){
    alert("Successfully imported JSON to DB; equivalent to "+count+" SQL statements");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Imported "+current+"/"+total+" statements";
};
cordova.plugins.sqlitePorter.importJsonToDb(db, json, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn
});

Update an existing database

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var json = {
    "data":{
        "inserts":{
            "Artist":[
                {
                    "Id":"1",
                    "Title":"Fred"
                }
            ]
        },
        "updates":{
            "Artist":[
                {
                    "set":
                        {
                            "Title":"Susan"
                        },
                    "where":
                        {
                            "Id":"2"
                        }
                    }
                ]
            },
            "deletes":{
                "Artist":[
                    {
                        "Id":"5"
                    }
                ]
            }
        }
    }
};
var successFn = function(count){
    alert("Successfully imported JSON to DB; equivalent to "+count+" SQL statements");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Imported "+current+"/"+total+" statements";
};
cordova.plugins.sqlitePorter.importJsonToDb(db, json, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn
});

exportDbToJson()

Exports a SQLite DB as a JSON structure

cordova.plugins.sqlitePorter.exportDbToJson(db, opts);

Parameters

  • {Database} db - open SQLite database to export
  • {object} opts - optional parameters:
    • {function} successFn - callback function to execute after export is complete, with arguments:
      • {object} json - exported JSON structure.
      • {integer} count - number of SQL statements that exported JSON structure corresponds to.
    • {boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false.

Example usage

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var successFn = function(json, count){
        console.log("Exported JSON: "+json);
        alert("Exported JSON contains equivalent of "+count+" SQL statements");
    };
cordova.plugins.sqlitePorter.exportDbToSql(db, {
    successFn: successFn
});

wipeDb()

Wipes all data from a database by dropping all existing tables.

cordova.plugins.sqlitePorter.wipeData(db, opts);

Parameters

  • {Database} db - open SQLite database to wipe
  • {object} opts - optional parameters:
    • {function} successFn - callback function to execute once wipe is complete, called with arguments:
      • {integer} count - number of tables dropped.
    • {function} errorFn - callback function to execute on error during wipe, called with arguments:
      • {object} error - object representing the error.
    • {function} progressFn - callback function to execute after each successful table drop, called with arguments:
      • {object} count - number of tables dropped so far.
      • {integer} totalCount - total number of tables to drop.

Example usage

var db = window.openDatabase("Test", "1.0", "TestDB", 1 * 1024);
var successFn = function(count){
    alert("Successfully wiped "+count+" tables");
};
var errorFn = function(error){
    alert("The following error occurred: "+error.message);
};
var progressFn = function(current, total){
    console.log("Wiped "+current+"/"+total+" tables";
};
cordova.plugins.sqlitePorter.wipeData(db, {
    successFn: successFn,
    errorFn: errorFn,
    progressFn: progressFn
});

JSON import optimisations

The JSON structure passed to the importJsonToDb() function is parsed in order to generate corresponding SQL commands. In doing so, the following optimisations have been made to minimize time taken to import large amounts of data:

Batch inserts

Using UNION SELECT syntax (see this stackoverflow post for details), INSERTS are grouped by up to 500 in a single SQL statement. This leads to significant performance gains when bulk importing data as to populate a database

For example, in the example project illustrating use of this plugin, the complex database example is actually the Chinook database - a sample database which contains over 15,000 INSERTS in the SQL file. Running the example project on my Samsung Galaxy S4, importing this SQL file takes around 300 seconds (5 mins). Whereas the JSON equivalent (using UNION SELECTs) contains only 17 INSERT statements and importing this takes around 3 seconds - 100 times faster!

Note: when using the importSqlToDb(), you must make any optimisations in your SQL.

Example projects

HTML5 WebSQL

https://github.com/dpa99c/cordova-sqlite-porter-example

This example project illustrates how the plugin can be used to import/export data from a WebSQL database in the WebView.

Native SQLite

https://github.com/dpa99c/cordova-sqlite-porter-example-native-plugin

This example project illustrates how the plugin can be used to import/export data from a native SQLite database using a native SQLite plugin

License

================

The MIT License

Copyright (c) 2015 Working Edge Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 14 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc