索引数据库 - 数据库在被删除并给出另一个名称后未定义

时间:2012-05-13 17:11:46

标签: indexeddb

我有以下代码创建一个数据库,并允许我对它执行基本操作,通常工作正常(我知道它不是最优雅的代码,我只是在一分钟测试分配:P).. ...但是,当我使用删除功能并刷新页面(包括擦除所有浏览数据等)后,数据库不再打开,我得到一个未定义的错误,任何想法为什么会发生这种情况?

P.S。我知道数据库有diff名称而不是删除功能,这是我试图创建一个新的

// Browser support detection
    var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

    // Transaction object for use with CRUD
    var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;



    (function () {     

        var db;

        var memberInfo = [
            { memID: "001", forename: "John", surname: "Smith", dob: "23/01/1987", password: "password123", gID: "1" },
            { memID: "002", forename: "Bob", surname: "Smith", dob: "17/05/1934", password: "password123", gID: "2" },
            { memID: "003", forename: "Frank", surname: "Smith", dob: "17/05/1990", password: "password123", gID: "2" },
            { memID: "004", forename: "Alan", surname: "Smith", dob: "17/05/1985", password: "password123", gID: "1" },
            { memID: "005", forename: "Tony", surname: "Smith", dob: "17/05/1978", password: "password123", gID: "3" }

        ];



        var gradeInfo = [
            { gID: "1", type: "GOLD", cost: "£200.00" },
            { gID: "2", type: "SILVER", cost: "£125.00" },
            { gID: "3", type: "BRONZE", cost: "£75.00" }

        ];

        var facInfo = [
            { memID: "001", factype: "Squash"},
            { memID: "001", factype: "Martial Arts"},
            { memID: "002", factype: "Swimming"},
            { memID: "004", factype: "Squash"},
            { memID: "004", factype: "Tennis"},
            { memID: "004", factype: "Swimming"},
            { memID: "005", factype: "Football"},
            { memID: "005", factype: "Archery"}

        ];




        function initDb() {


            // Open call is used to open database with given name & set DB version
            var request = indexedDB.open("HaleSportsClub", 1); 

            // Function used to get database object from request and store for user
            request.onsuccess = function (evt) {
                db = request.result;                                                
            };

            request.onerror = function (evt) {
                alert("Database did not open: " + evt.target.message); // !!!!!
            };

            // Fire when version is smaller than one provided (by open call)
            request.onupgradeneeded = function (evt) { 

                var db = evt.target.result;

                // Each object store resembles a table in RDBMS              
                var memberObject = db.createObjectStore("members", { keyPath: "memID" });
                var gradeObject = db.createObjectStore("grades", { keyPath: "gID" });
                var facObject = db.createObjectStore("facilities", { keyPath: "memID" });

                // Creates searchable indexes (??)

                memberObject.createIndex("forename", "forename", { unique: false });
                memberObject.createIndex("surname", "surname", { unique: false });
                memberObject.createIndex("gID", "gID", { unique: false });
                facObject.createIndex("memID", "memID", { unique: false });


                for (i in memberInfo) {
                    memberObject.add(memberInfo[i]);
                }


                for (i in gradeInfo) {
                    gradeObject.add(gradeInfo[i]);
                }

                for (i in facInfo) {
                    facObject.add(facInfo[i]);
                } 



            };

            request.onblocked = function (evt){

                alert("database currently being upgraded, try again soon");
            }

        }



        function contentLoaded() {

            initDb();    

//此区域包含添加/删除等功能,但我已将此问题用于此问题

        }



        window.addEventListener("DOMContentLoaded", contentLoaded, false); // starts functions

})(); 

我使用此代码删除数据库:

   function delDB() {

                    var dbreq = indexedDB.deleteDatabase("HSC_Member_Database");

                    try{
                        dbreq.onsuccess = function (event) {
                            var output = document.getElementById("printOutput");
                            output.innerHTML = "deleted";

                        }

                        dbreq.onerror = function (event) {
                            console.log("Database error code: " + event.target.errorCode);
                        }

                    }catch(e){
                        console.log("Database error code: " + e.target.errorCode);
                    }

            }

1 个答案:

答案 0 :(得分:1)

升级后遇到问题时要尝试的一件事是增加数据库版本号:

var request = indexedDB.open("HaleSportsClub", 2);