使用索引和游标结合怎么添加数据
我这样写为啥不行 是因为没有这个方法cursor.put()还是我写错了?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//数据库
var dba = indexedDB.open("ZYC", 3);
dba.onsuccess = function () {
console.log(1);
}
dba.onupgradeneeded = function () {
// console.log("版本升级了");
var tbl = dba.result;
// console.log(tbl)
// tbl.createObjectStore("test3",{autoIncrement:true});
var index = tbl.createObjectStore("tests", { keyPath: "id" });
index.createIndex("index", "name", { unique: true });
}
var json = [{
"id": 1001,
"name": "zyc1",
"age": 18
}, {
"id": 1002,
"name": "zyc2",
"age": 18
}, {
"id": 1003,
"name": "zyc3",
"age": 18
}, {
"id": 1004,
"name": "zyc4",
"age": 18
}]
setTimeout(function () {
//找到数据库
var db = dba.result;
//找到表
var transcation = db.transaction("tests", "readwrite");
var store = transcation.objectStore("tests");
var requestNode = store.openCursor(IDBKeyRange.upperBound(1004));
// for(var i=0;i<json.length;i++){
// store.add(json[i]);
// }
requestNode.onsuccess = function () {
var cursor = requestNode.result;
console.log(cursor);
//添加数据
cursor.put({
"id": 1001,
"name": "zyc1",
"age": 18
}).onsuccess=function(){
console.log("添加成功");
}
if (cursor) {
if (cursor.value.name == "zyc1111") {
//更新
// cursor.update({
// "id": 1001,
// "name": "zyc1111",
// "age": 18
// });
//删除
// cursor.delete().onsuccess=function(){
// console.log("删除成功");
// }
}
console.log(cursor.value);
cursor.continue();
}
};
}, 3000)
</script>
</body>
</html>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 622 份
- 解答问题 6815 个
微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星