データベースを作成し、通常は正常に動作する基本的な操作を実行できる次のコードがあります(最もエレガントなコードではないことはわかっていますが、すぐに割り当てをテストしているだけです: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);
}
}