我正在将IndexedDB用于测试项目。这是一些示例代码:
var indexedDB = window.indexedDB || window.webkitIndexedDB
||window.mozIndexedDB||window.msIndexedDB;
var request = indexedDB.open("mydb",2),
customerData=[
{ssn:"444-44-4444",name:"Bill",age:35,email:"bill@company.com"},
{ssn:"555-55-5555",name:"Donna",age:32,email:"donna@home.org"}
];
request.onerror = function(event){
};
request.onupgradeneeded = function(event) {
var objectStore = db.createObjectStore("customers",{keyPath:"ssn"});
objectStore.createIndex("name","name",{unique:false});
objectStore.createIndex("email","email",{unique:true});
for(var i in customerData){
objectStore.add(customerData[i]);
}
};
request.onsuccess = function(e) {
};
我真正不明白的是我的请求对象何时运行onupgradeneeded
而不是onsuccess
(假设当然没有错误)。是在没有对象存储的时候吗?或者当创建一个新的数据库版本时?
谢谢