
Mozilla has developed a special database for storing the data for the Firefox OS applications. It is simple IndexedDB.
From Mozilla Developers Network we can know what is IndexedDB .If you are worked only with Relational Database Management System, then for the first time it will be very difficult to use. But you can easily master this database since you work more with Javascript while developing mobile applications for Firefox.
IndexedDB is an API for client-side storage of significant amounts of structured data and for high performance searches on this data using indexes. While DOM Storage is useful for storing smaller amounts of data, it is less useful for storing larger amounts of structured data. IndexedDB provides a solution.
Below is the simple code which will initialize the database when the application gets started.
function initializeDB() {
if (window.indexedDB) {
console.log("IndexedDB support is there");
}
else {
alert("Indexed DB is not supported. ");
}
// open the database
// 1st parameter : Database name. We are using the name 'notesdb'
// 2nd parameter is the version of the database.
var request = indexedDB.open('notesdb', 1);
request.onsuccess = function (e) {
// e.target.result has the connection to the database
db = e.target.result;
//Alternately, if you want - you can retrieve all the items
}
request.onerror = function (e) {
console.log(e);
};
// this will fire when the version of the database changes
// We can only create Object stores in a versionchange transaction.
request.onupgradeneeded = function (e) {
// e.target.result holds the connection to database
db = e.target.result;
if (db.objectStoreNames.contains("notes")) {
db.deleteObjectStore("notes");
}
// create a store named 'notes'
// 1st parameter is the store name
// 2nd parameter is the key field that we can specify here. Here we have opted for autoIncrement but it could be your
// own provided value also.
var objectStore = db.createObjectStore('notes', { keyPath: 'id', autoIncrement: true });
console.log("Object Store has been created");
};
}
Below line of code will help you to add the elements in the database
function addItem() {Below lines of code will allow you to show the database contents
// create the transaction with 1st parameter is the list of stores and the second specifies
// a flag for the readwrite option
var transaction = db.transaction([ 'notes' ], 'readwrite');
//Create the Object to be saved i.e. our Note
var value = {};
value.title = title;
value.details = ing;
value.noteDes = desc;
// alert("hai");
// add the note to the store
var store = transaction.objectStore('notes');
var request = store.add(value);
request.onsuccess = function (e) {
alert("Added Successfully");
};
request.onerror = function (e) {
alert("Error " + e.value);
}
}
function display()
{
//Read the notes
var transaction = db.transaction(["notes"]);
var objectStore = transaction.objectStore("notes")
// open a cursor to retrieve all items from the 'notes' store
objectStore.openCursor().onsuccess = function (e) {
//document.getElementById("note-list").innerHTML='';
var cursor = e.target.result;
if (cursor) {
var value = cursor.value;
var para=document.createElement("h1");
var node=document.createTextNode(value.title);
para.appendChild(node);
var element=document.getElementById("note-list");
element.appendChild(para);
var para=document.createElement("p");
var node=document.createTextNode(value.details);
para.appendChild(node);
var element=document.getElementById("note-list");
element.appendChild(para);
var para=document.createElement("p");
var node=document.createTextNode(value.noteDes);
para.appendChild(node);
var element=document.getElementById("note-list");
element.appendChild(para);
// move to the next item in the cursor
cursor.continue();
}
};
}
The above code will help you to learn more about how to start the database, add entries and display the entries from the database. It is very easy to use and is very simple to handle.