Storage

The Storage feature enables your mobile application to access the following storage mechanisms:

 1. SQLite Storage

This feature enables your mobile application to access SQLite databases. It enables you to store data in a structured database that can be queried using a standard SQL syntax.

Supported Platforms

This feature is supported in the following platforms:

  • iOS
  • Android

Installation

Execute the following command in your Cordova project directory:

cordova plugin add cordova-sqlite-storage

Usage

Assume that there is a "SQL LITE" button on your mobile application that will use SQLite when clicked. The following is a sample code snippet that can used in the listener JS file for invoking SQLite storage in the mobile application.

/* The following function opens the SQLite database and executes a SQL query.
*  Here CFEC.CLICK is the Canvas form event for the button click.
*  SQLITE is the form item ID for the "SQL LITE" button.
*/
this.fm.registerHandler(CFEC.CLICK, "SQLITE", 
                        function (fm, event, fieldName, value) 
						{
            				var params = 
							{
                				name: 'demo.db',
								location: 'default'
            				};
            				canvas.dbReference = canvas.env.storage.sqlite.openDatabase(params);
            				canvas.env.storage.sqlite.executeSql
							( 	canvas.dbReference, 
								'CREATE TABLE CANVAS (USER TEXT)', 
								[], 
								successCB, 
								errorCB
							);
						});

// The following function is called when SQL query successfully executed.
function successCB(success) 
{
    showAlert("success");
}

// The following function is called when SQL query is not successfully executed.
function errorCB(error) 
{
    showAlert("error");
}


 2. Web SQL Storage

This feature enables your mobile application to access Web SQL storage. It enables you to store data in a structured database that can be queried using a standard SQL syntax.

Usage

Assume that there is a "Web SQL" button on your mobile application that will use Web SQL when clicked. The following is a sample code snippet that can used in the listener JS file for invoking Web SQL storage in the mobile application.

/* This function opens the WebSQL database and executes a SQL query.
*  Here CFEC.CLICK is the Canvas form event for the button click.
*  WEBSQL is the form item ID for the "Web SQL" button.
*  
this.fm.registerHandler(CFEC.CLICK, "WEBSQL", 
                        function (fm, event, fieldName, value) 
						{
            				var params = 
							{
                				name: 'canvas.db',
								version: '1.0',
								textDescription: 'demo',
								size: '2 * 1024 * 1024'
            				};
            				canvas.dbReferenceWeb = canvas.env.storage.websql.openDatabase(params);
            				canvas.env.storage.sqlite.executeSql
							(
								canvas.dbReferenceWeb, 
								'CREATE TABLE CANVAS (USER TEXT)', 
								[], 
								successCB, 
								errorCB
							);
						});

// The following function is called when SQL query successfully executed.
function successCB(success) 
{
    showAlert("success");
}

// The following function is called when SQL query is not successfully executed.
function errorCB(error) 
{
    showAlert("error");
}


 3. Session Storage

This feature enables your mobile application to access session storage.

Usage

Assume that there is a "SESSION STORAGE" button on your mobile application that will use session storage when clicked. The following is a sample code snippet that can used in the listener JS file for invoking session storage in the mobile application.

/* This function stores data in session storage as key-value pair combination.
*  Here CFEC.CLICK is the Canvas form event for the button click.
*  SESSIONSTORAGE is the form item ID for the "SESSION STORAGE" button.
*/
this.fm.registerHandler(CFEC.CLICK, "SESSIONSTORAGE", 
                        function (fm, event, fieldName, value) 
						{
            				var params = 
							{
                				key: 'name',
								value: 'Canvas'
            				};
            				canvas.env.storage.session.setItem(params);
            				var val = canvas.env.storage.session.getItem('name');
            				showAlert(val);
						});


 4. Local Storage

This feature enables your mobile application to access local storage. It provides a simple and synchronous key-value pair storage.

Usage

Assume that there is a "LOCAL STORAGE" button on your mobile application that will use local storage when clicked. The following is a sample code snippet that can used in the listener JS file for invoking local storage in the mobile application.

/* This function stores data in local storage as key-value pair combination.
*  Here CFEC.CLICK is the Canvas form event for the button click.
*  LOCALSTORAGE is the form item ID for the "LOCAL STORAGE" button.
*/
this.fm.registerHandler(CFEC.CLICK, "LOCALSTORAGE", 
                        function (fm, event, fieldName, value) 
						{
            				var params = 
							{
                				key: 'name',
								value: 'Canvas'
            				};
            				canvas.env.storage.local.setItem(params);
            				var lval = canvas.env.storage.local.getItem('name');
            				showAlert(lval);
						});