View on GitHub

Cordova-plugin-websqldatabase-initializer

Cordova plugin for WebSqlDatabase initialization

Download this project as a .zip file Download this project as a tar.gz file

This is a Cordova 3 plugin (using the new plugin management system) that enables initialization for WebSqlDatabase.

If you want your PhoneGap application to start with a preloaded WebSqlDatabase, this plugin will simplify it for you.

This plugin is compatible with iOS, Android and WP (7 & 8).

Install

Using the Cordova 3 Command-line Interface:

cordova plugin add org.smile.websqldatabase.initializer

If you are not using the Cordova Command-line Interface, follow Using Plugman to Manage Plugins.

Create the database containing initialization data

The database used for initialization (named "myDemoSQLiteDB" below) is an SQLite file with a ".db" extension compressed in a ".zip" file.

You can create the ".db" file with the initial content via Chrome/Chromium using the WebSqlDatabase API (and retrieve it from the "databases" diretory of the User Data Directory) or with an SQLite client (sqlite3, sqlitebrowser, ...).

Name the resulting database binary file with a ".db" extension and zip it.

Databases.db

For iOS and Android, a Databases.db file is required to "index" the available databases.

To create this file, use an SQLite client (sqlite3, sqlitebrowser, ...) and execute the following code (replacing "myDemoSQLiteDB.db" with the correct value):

CREATE TABLE Databases (guid INTEGER PRIMARY KEY AUTOINCREMENT, origin TEXT, name TEXT, displayName TEXT, estimatedSize INTEGER, path TEXT);
INSERT INTO Databases VALUES(1, 'file__0', 'myDemoSQLiteDB.db', 'Proto DB', 1000000, 'myDemoSQLiteDB.db');
CREATE TABLE Origins (origin TEXT UNIQUE ON CONFLICT REPLACE, quota INTEGER NOT NULL ON CONFLICT FAIL);
INSERT INTO Origins VALUES('file__0',52428800);

Usage

Android

import org.smile.websqldatabase.*;
@Override
public DatabaseConfig getDatabaseConfig() {
    return new DatabaseConfig("myDemoSQLiteDB.zip", "myDemoSQLiteDB.db");
}
@Override
public void loadWebApp() {
    super.loadUrl(Config.getStartUrl());
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    DatabaseInitializer.load(this);
}

iOS

#import "LoadDatabase.h"
#import "DatabaseConfig.h"
[[LoadDatabase new] load:[[DatabaseConfig alloc] init:@"myDemoSQLiteDB.zip" secondValue:@"myDemoSQLiteDB.db"]];

Windows Phone (7 & 8)

using org.smile.websqldatabase;
new DatabaseLoader().LoadDatabase(new DatabaseConfig(@"myDemoSQLiteDB.zip", @"myDemoSQLiteDB.db"));

Usage in JavaScript

Load the database with the following code:

window.openDatabase("myDemoSQLiteDB.db", "1.0", "Proto DB", 1000000);

Example

A prototype including this plugin is available at https://github.com/Smile-SA/cordova-plugin-websqldatabase-initializer-proto

External library

This plugin uses the following libraries: