I am using indexedDB of HTML5. I have problem in creating second object store and make transactions. First object store works fine but the second one is not working. I have tried two approaches; both are not working correctly.
In first approach second object store creates but it takes the properties(such as keypath, autoincrement) of the first object store. My Second Object Store does not make more than one transaction in this approach.
Here is First Approach
var request, secondRequest, db, version;
var database;
if (!window.indexedDB) {
alert("Your Browser does not support IndexedDB");
}
else {
var dbName = "MyStatus";
request = window.indexedDB.open(dbName, 2);
request.onerror = function(event) {
alert("Error opening DB", event);
};
request.onupgradeneeded = function (event) {
alert("Upgrading");
db = event.target.result;
var tblIncident = db.createObjectStore("Incident", { keyPath: "MyID", unique: true });
var tblIncidentStatus = db.createObjectStore("tblVolunteerIncidentStatus",{ keyPath: "ID", unique: false, autoincrement: true });
};
request.onsuccess = function(event) {
alert("Success opening DB");
db = event.target.result;
alert(db);
$(document).trigger('insert');
};
}
$(document).bind('insert', function () {
var incidID = 0;
var transaction = db.transaction(["Incident"], "readwrite");
transaction.oncomplete = function (event) {
alert("Case Inserted Successfully :)");
incidID++;
};
transaction.onerror = function (event) {
alert("Case Insertion Error :(");
};
var oStoreIncident = transaction.objectStore("Incident");
oStoreIncident.add({ INCID: incidId, INCDETAILS: "" });
});
$(document).bind('insertStatus', function () {
var incidID = 0;
var transaction = db.transaction(["IncidentStatus"], "readwrite");
transaction.oncomplete = function (event) {
alert("Case Inserted Successfully :)");
incidID++;
};
transaction.onerror = function (event) {
alert("Case Insertion Error :(");
};
var oStoreIncident = transaction.objectStore("IncidentStatus");
oStoreIncident.add({ INCID: incidId, INCDETAILS: "" });
});
Then I followed this link How to Create Multiple Object Stores
and using this my second object store not creating therefore my transactions on this are also not working.
Here is Second Approach
if (!window.indexedDB) {
alert("Your Browser does not support IndexedDB");
}
else {
var dbName = "MyStatus";
request = window.indexedDB.open(dbName, 2);
request.onerror = function(event) {
alert("Error opening DB", event);
};
request.onupgradeneeded = function (event) {
alert("Upgrading");
db = event.target.result;
var tblIncident = db.createObjectStore("Incident", { keyPath: "MyID", unique: true });
};
request.onsuccess = function(event) {
alert("Success opening DB");
db = event.target.result;
alert(db);
version = parseInt(db.version);
db.close();
request2 = window.indexedDB.open(dbName, 3);
request2.onupgradeneeded = function (evnt) {
alert("on upgrade needed");
db = evnt.target.result;
alert("DB: " + db);
var tblIncidentStatus = db.createObjectStore("IncidentStatus");
alert(tblIncidentStatus);
};
request2.onsuccess = function (evnt) {
db = evnt.target.result;
alert("Second Table Created Successfully");
};
request2.onerror = function (evnt) {
db = evnt.target.result;
alert("Second Table not creating " + db);
};
$(document).trigger('insert');
$(document).trigger('insertStatus');
};
}
$(document).bind('insert', function () {
var incidID = 0;
var transaction = db.transaction(["Incident"], "readwrite");
transaction.oncomplete = function (event) {
alert("Case Inserted Successfully :)");
incidID++;
};
transaction.onerror = function (event) {
alert("Case Insertion Error :(");
};
var oStoreIncident = transaction.objectStore("Incident");
oStoreIncident.add({ INCID: incidId, INCDETAILS: "" });
});
$(document).bind('insertStatus', function () {
var incidID = 0;
var transaction = db.transaction(["IncidentStatus"], "readwrite");
transaction.oncomplete = function (event) {
alert("Case Inserted Successfully :)");
incidID++;
};
transaction.onerror = function (event) {
alert("Case Insertion Error :(");
};
var oStoreIncident = transaction.objectStore("IncidentStatus");
oStoreIncident.add({ INCID: incidId, INCDETAILS: "" });
});
I am new to indexedDB; if anyone has solution so please share.
Thanks in advance.