Introduction
We can provide access to the device storage options with the help of Storage in Apache Cordova/PhoneGap. The method that we use is openDatabase and the arguments are:
database_name
database_version
database_displayname
database_size
openDatabase: It returns the new database object. window.openDatabase returns a new Database object. This method will create the new SQL Lite database object and we can use this object to manipulate the data in the database. The supported platforms are Android, BlackBerry WebWorks (OS 6.0 and higher), iPhone and webOS.
Syntax
var dbShell = window.opwnDatabase(database name, database version, database displayname, database size);
The database_name parameter specifies the name of the database, database_version specifies the version name, database_displayname specifies the display name of the database and the database_size defines the size of the database.
For example
var db=window.openDatabase("test","1.0","Test DB",1000000);
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>Cordova Database Example</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8"/>
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
var db = window.openDatabase("test", "1.0", "Test DB", 1000000);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Open Database</p>
</body>
</html>
Database: The database object contains the methods that allow the user to manipulate the Database. It has the two methods transaction and changeVersion. The transaction runs a database transaction and the changeVersion method allows scripts to automaticly verify the version number and change it at the same time as doing a schema update. The supported platforms are Android, BlackBerry WebWorks(OS 6.0 and higher), iPhone and webOS.
Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Cordova Database Example</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title"
charset="utf-8" />
<script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
<script type="text/javascript">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
// Populate the database
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}
// Transaction error callback
function errorCB(tx, err) {
alert("Error processing SQL: " + err);
}
// Transaction success callback
function successCB() {
alert("success!");
}
</script>
</head>
<body>
<h1>
Example</h1>
<p>
Database</p>
</body>
</html>
Summary : In this article I explained how to use storage in Cordova/PhoneGap.