1

我正在使用最新的 google chrome..我正在尝试实现 HTML5 indexedDB 示例。AIM 是在页面加载并在其中存储数据时创建数据库。如果有人有,请发布任何示例工作示例。我尝试了这段代码,但是Uncaught Error: InvalidStateError: DOM IDBDatabase Exception 11当我尝试创建交易时却得到了。这是代码片段..

var idbRequest;
    var idb;
$(document).ready(function(){
    //ini----------------------
     var peopleData = [
                       { name: "John Dow", email: "john@company.com" },
                       { name: "Don Dow", email: "don@company.com" }
                   ];
    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
    //create database.............
    if (window.indexedDB) {
          idbRequest = window.indexedDB.open("StoreDB");
          idbRequest.onsuccess =  function(e) {
              idb = idbRequest.result || e.result;  // FF4 requires e.result. IDBRequest.request isn't set :(
          /*idbRequest.onerror = function (evt) {
                  console.log("IndexedDB error: " + evt.target.errorCode);
              };*/
              var v = '2.0';
                var setVrequest = idb.setVersion(v);
                setVrequest.onsuccess = function(e) {
                    if(idb.objectStoreNames.contains("Stores")) {
                        alert("ObjectStore is already created.");
                        return false;
                      }
                    var objectstore = idb.createObjectStore("Stores");
                    alert("Object store Created.");
                    if (!idb.objectStoreNames.contains('Stores')) {
                          alert("Object store doesn't exist.");
                        return;
                      }else{
                          alert("Object store contain 'store'");
                      }

                       // Create a transaction that locks the world.
                     var trans = idb.transaction(["Stores"],"readwrite");//getting exception on this line..
                     trans.oncomplete = function(){
                          console.log("Success transaction");
                        };
                        var objectStore = trans.objectStore("Stores");
                      var request = objectStore.put(
                          1,
                          "wsdsdsd");
                    alert("data added");
                };






            };


    }   
});
4

2 回答 2

3

解决了..我收到错误,因为在版本更改正在进行时我们无法创建事务..这是最终的工作答案..

var idbRequest;
    var idb;
$(document).ready(function(){
    //ini----------------------

    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB;
    window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
    window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
    //create database.............
    if (window.indexedDB) {
          idbRequest = window.indexedDB.open("DB");
          idbRequest.onsuccess =  function(e) {
              idb = idbRequest.result || e.result;  // FF4 requires e.result. IDBRequest.request isn't set :(
          /*idbRequest.onerror = function (evt) {
                  console.log("IndexedDB error: " + evt.target.errorCode);
              };*/
              var v = '2.0';
                var setVrequest = idb.setVersion(v);
                setVrequest.onsuccess = function(e) {
                    if(idb.objectStoreNames.contains("Stores")) {
                        alert("ObjectStore is already created.");
                        var vertrans = setVrequest.result;
                        vertrans.oncomplete = doStuff;
                        return false;
                      }
                    var objectstore = idb.createObjectStore("Stores");
                    alert("Object store Created.");
                    if (!idb.objectStoreNames.contains('Stores')) {
                          alert("Object stores doesn't exist.");
                        return;
                      }else{
                          alert("Object store contain 'stores'");
                      } 
                    var vertrans = setVrequest.result;
                    vertrans.oncomplete = doStuff;
                };

            };


    }   
});

function doStuff(){
       // Create a transaction that locks the world.
    var trans = '';

         trans = idb.transaction(['Stores'],'readwrite');

     var objectStore = trans.objectStore('Stores');
      var request = objectStore.put(
          1,
          "wsdsdsd");
    alert("data added");
     trans.oncomplete = function(){
          console.log("Success transaction");
        };
}
于 2012-08-20T12:01:45.340 回答
0

您可以在我的网站上找到一个有效的 indexeddb 示例。我正在使用我为 indexeddb API 编写的库,名为linq2indexeddb。试一试,它将为您节省大量精力,使您的代码 crossborwser 兼容。

于 2012-08-20T12:05:26.047 回答