我有以下代码可以创建一个数据库并允许我对其执行基本操作,这通常可以正常工作(我知道它不是最优雅的代码,但我只是在一分钟内测试分配:P).....但是,当我使用删除功能并刷新页面(包括在擦除所有浏览数据等之后)时,数据库不会再次打开并且我得到一个未定义的错误,关于为什么会发生这种情况的任何想法?
ps 我知道数据库的名称与删除函数中的名称不同,这是我试图创建一个新的
// 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);
}
}