Professional JavaScript for Web Developers 3rd edition を読んで、例を試しています。第 23 章には、デモがあります: IndexedDBExample02.htm。エラーがあります: DOM IDBDatabase Exception 8 助けてください、どうもありがとうございました!
(function(){
var indexedDB = window.indexedDB || window.msIndexedDB || window.mozIndexedDB || window.webkitIndexedDB,
request,
store,
database,
users = [
{
username: "007",
firstName: "James",
lastName: "Bond",
password: "foo"
},
{
username: "ace",
firstName: "John",
lastName: "Smith",
password: "bar"
}
];
request = indexedDB.open("example");
request.onerror = function(event){
alert("Something bad happened while trying to open: " + event.target.errorCode);
};
request.onsuccess = function(event){
database = event.target.result;
initializeDatabase();
};
function initializeDatabase(){
if (database.version != "1.0"){
request = database.setVersion("1.0");
request.onerror = function(event){
alert("Something bad happened while trying to set version: " + event.target.errorCode);
};
request.onsuccess = function(event){
store = database.createObjectStore("users", { keyPath: "username" });
var i=0,
len = users.length;
while(i < len){
store.add(users[i++]);
}
alert("Database initialized for first time. Database name: " + database.name + ", Version: " + database.version);
};
} else {
alert("Database already initialized. Database name: " + database.name + ", Version: " + database.version);
request = database.transaction("users").objectStore("users").get("007");
request.onsuccess = function(event){
alert(event.target.result.firstName);
};
}
}
})();