Home

Awesome

CordovaSQLite

A custom plugin for accessing SQLite databases through Cordova. The plugin API is different from the WebSQL API that is used in other plugins.

Installation

Use cordova CLI to install the plugin:

<pre> > cordova plugin add net.orworks.cordovaplugins.cordovasqlite > OR, cordova plugin add https://github.com/samikrc/CordovaSQLite.git </pre>

Uninstallation

Use cordova CLI to uninstall the plugin:

<pre> > cordova plugin rm net.orworks.cordovaplugins.cordovasqlite </pre>

API

The API contains the following functions:

Sample Code

The following code assumes an existing database file. Creating a new database file with some data would work the same way.

<pre> window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (confDir) { console.log("Got directory: " + confDir.fullPath); var dbFullPath = cordova.file.externalDataDirectory + "2014110801.sqlite"; confDir.getFile("2014110801.sqlite", { create: false }, function (confFile) { console.log("Got file: " + confFile.fullPath); cordovaSQLite.openDatabase(dbFullPath, false, function () { cordovaSQLite.execQueryArrayResult("select value from info where name=?", ["Conference"], function (confName) { console.log("Got conference name: " + confName); }, function (error) { alert("##execQueryArrayResult: " + error); }); }, function (error) { alert("##openDatabase: " + error); }); }, function (error) { alert("##getFile: " + error); }); }, // Success callback [resolveLocalFileSystemURL] function (error) { alert(error); }); // Failure callback [resolveLocalFileSystemURL] </pre>

Platform Notes

iOS

For iOS platform, after you add the plugin, you will also have to add "libsqlite3.0.dylib" for linking. To do that, perform the following:

browser

<pre> ---------------------------------------- [Update] The implementation is now broken on Firefox browsers (from v36.0 onwards). From my research, it seems like FF has changed some of their add-on interface, which is resulting in the addon mentioned below ("HTML5 WebSQL for Firefox" plugin v0.6) to not work any more. ----------------------------------------- </pre>

One of the important aspect of developing apps with Cordova is that one should be able to test out the app completely from a browser environment, without having to build for a platform everytime. Unfortunately, many plugins lack the components for browser. This plugin can be tested out in a compatible browser environment. This is tested on Firefox (v34.0.5) and on Chrome (Version 42.0.2311.152 m).

There are prerequisites to running the plugin in a browser environment.

Firefox

The databases will be available at the following locations:

In order to use database in the browser for testing your app, follow one of the strategies below:

Chrome

The database will be available at the following location:

In order to use database in the browser for testing your app, do as follows:

<pre> $ sqlite3.exe Databases.db -header "select * from Databases" id|origin|name|description|estimated_size 1|file__0|2014110801|Session DB|2097152 2|file__0|/2014110801.sqlite|Session DB|2097152 3|http_playground.html5rocks.com_0|Todo|Todo manager|5242880 4|file__0|smscollection|Session DB|2097152 </pre>