var request = indexedDB.open('dzxcx',3);
request.onerror = function(event) {
alert('失败' + event.target.errorCode);
}
var users = [
{
username : "zhangsan",
password : "zhangsan",
email : "zhangsan@126.com"
},
{
username : "lisi",
password : "lisi",
email : "lisi@126.com"
}
]
request.onupgradeneeded = function(event) {
var db = event.target.result;
var ObjectStore = db.createObjectStore("users",{keyPath:"username"});
ObjectStore.createIndex("username","username",{unique : false});
ObjectStore.createIndex("password","password",{unique : false});
ObjectStore.createIndex("email","email",{unique : true});
for(var i in users) {
ObjectStore.add(users[i])
}
}
function addContent() {
request.onerror = function(e) {
alert("错误");
}
request.onupgradeneeded = function(e) {
var db = event.target.result;
var transaction = db.transaction(["users"],"readwrite");
transaction.oncomplete = function(event) {
alert("All done!");
};
var objectStore = transaction.objectStore("users");
for (var i in users) {
var request = objectStore.add(users[i]);
request.onsuccess = function(event) {
// event.target.result == customerData[i].ssn
alert("成功")
};
}
}
}
addContent();
为什么我通过这一段代码 我的indexDB里还是没有数据什么的啊 只有
而且这个 transaction.objectStore 这个objectStore是从哪里来的 是什么意思啊? 简单点来说
我就是想了解 怎么插入数据 删除数据 获取数据 求帮助啊!
我的是直接保错
Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.
查了一下是因为你重复使用 request.onupgradeneeded 了
把最后2段注释掉就可以了啊
结果展示: