Database connection
Simple trick for us to interact Intel XDK/cordova projects with database. simply add indexedDB plugin to your project and use the following code snippets
Requesting Access to IndexedDB and Creating a Object Store
//provide database name and version number
var request = indexedDB.open(“habiz”, 1);
var db = null;
request.onupgradeneeded = function(){
db = request.result;
//create object store and define key property of the objects on that store i.e., primary index. Here "ID" is the key
var store = db.createObjectStore("habiz_store", {keyPath: "ID",autoIncrement: false});
//define other properties of the objects of that store i.e., define other columns.
store.createIndex("name", "name", {unique: true});
store.createIndex("age", "age", {unique: false});
store.createIndex("serialnumber", "serialnumber", {unique: true});
}
request.onsuccess = function(){
//database connection established
db = request.result;
}
request.onerror = function(){
console.log(“An error occured while connecting to database”);
}
Adding Objects to Object Store
var object1 = {name: “intelhub”, age: “6”, ID: “d3223”, serialnumber:1};
var object2 = {name: “habiz”, age: “2”, ID: “dasdasd121”,serialnumber: 2};
var write_transition = db.transaction(“habiz_store”, “readwrite”);
var store = write_transition.objectStore(“habiz_store”);
store.put(object1);
store.put(object2);
Find a Object
var read_transition = db.transaction(“habiz_store”, “readonly”);
var store = read_transition.objectStore(“habiz_store”);
var row = store.get(“d3223”);
row.onsuccess = function(evt){
console.log("Name is: " + row.result.name);
console.log("ID is: " + row.result.ID);
console.log("Age is: " + row.result.age);
console.log("Serial Number: " + row.result.serialnumber);
}
Get all Objects
var read_transition = db.transaction(“habiz_store”, “readonly”);
var store = read_transition.objectStore(“habiz_store”);
var rows = store.openCursor()
rows.onsuccess = function(evt){
var cursor = evt.target.result;
if(cursor)
{
console.log("Name is: " + cursor.value.name);
console.log("ID is: " + cursor.key);
console.log("Age is: " + cursor.value.age);
console.log("Serial Number: " + cursor.value.serialnumber);
cursor.continue();
}
}
Delete a Object
var write_transition = db.transaction(“habiz_store”, “readwrite”);
var store = write_transition.objectStore(“habiz_store”);
var deleted_row = store.delete(“d3223”);
deleted_row.onsuccess = function(event){
console.log(“Deleted”);
}
Delete all Objects
var write_transition = db.transaction(“habiz_store”, “readwrite”);
var store = write_transition.objectStore(“habiz_store”);
var delete_all_rows = store.clear();
delete_all_rows.onsuccess = function(event){
console.log(“Deleted all rows”);
}
Update Object
var object1 = {name: “Intelhub”, age: “3”, ID: “d3223”,serialnumber: 1};
var object2 = {name: “habiz”, age: “2”, ID: “dasdasd121”,serialnumber: 2};
var write_transition = db.transaction(“habiz_store”, “readwrite”);
var store = write_transition.objectStore(“habiz_store”);
store.put(object1);
store.put(object2);
you can check here for more tricks