IndexedDB - 无法创建多个对象存储并对其进行事务处理

时间:2015-11-18 12:15:05

标签: javascript html5 transactions indexeddb

我正在使用HTML5的indexedDB。我在创建第二个对象存储和进行事务时遇到问题。第一个对象存储工作正常,但第二个不工作。我试过两种方法;两者都不能正常工作。

在第一种方法中,第二个对象存储库创建但它接受第一个对象存储库的属性(例如keypath,autoincrement)。我的第二个对象存储在这种方法中不会进行多个事务。

这是第一种方法

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: "" });
     });

然后我点了这个链接How to Create Multiple Object Stores

使用这个我的第二个对象存储不创建因此我的交易也无法正常工作。

这是第二种方法

 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: "" });
     });

我是indexedDB的新手;如果有人有解决方案,请分享。

提前致谢。

2 个答案:

答案 0 :(得分:3)

使用此模式打开数据库连接并升级架构:

var open = indexedDB.open(dbName, 3);
open.onupgradeneeded = function(e) {
   var db = open.result;
   if (e.oldVersion < 1) {
       // initial database creation
       // (your code does nothing here)
   }
   if (e.oldVersion < 2) {
       // version 1 -> 2 upgrade
       db.createObjectStore("Incident", { keyPath: "MyID", unique: true });
       // ...
   }
   if (e.oldVersion < 3) {
       // version 2 -> 3 upgrade
       db.createObjectStore("IncidentStatus");
       // ...
   }
};
open.onsuccess = function(e) {
    var db = open.result;
    // db.version guaranteed to be 3 (or open would have failed)
};

答案 1 :(得分:1)

在第二个示例中,您无法向第二个对象库打开事务的原因是因为它尚未创建。您很快就会触发插入事件。您应该在request2的onsuccess回调中调用对象存储的insert触发器。

正如约书亚所提到的,你应该在一次通话中捆绑你的升级版本。您还可以在一个onupgradeneeded事件中创建多个对象存储。