- IndexedDB 教程
- IndexedDB - 主页
- IndexedDB - 簡介
- IndexedDB - 安装
- IndexedDB - 连接
- IndexedDB - 对象存储
- IndexedDB - 创建数据
- IndexedDB - 读取数据
- IndexedDB - 更新数据
- IndexedDB - 删除数据
- 使用 getAll() 函数
- IndexedDB - 索引
- IndexedDB - 范围
- IndexedDB - 事务
- IndexedDB - 错误处理
- IndexedDB - 搜索
- IndexedDB - 游标
- IndexedDB - Promise 包装器
- IndexedDB - Ecmascript 绑定
- IndexedDB 有用资源
- IndexedDB - 快速指南
- IndexedDB - 有用资源
- IndexedDB - 讨论
IndexedDB - 更新数据
在我们创建数据后,下一步是对它执行各种操作;所以我们需要定期更新数据。我们还需要在将数据错误输入数据库的情况下更新数据。这里,我们必须指定一个读写事务,因为我们想要写入数据库,而不仅仅是从数据库读取数据。
如果我们想要修改它或使数据库中已有的一个条目生效,我们使用此 put() 函数。
语法
var requestUpdate = objectStore.put(data);
我们使用 put() 对事务发生的且需要更新数据的对象存储上的函数。
示例
我们来看一下下面的脚本,了解如何使用 put() 函数更新或修改 objectstore 中的数据 −
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script>
const request = indexedDB.open("botdatabase",1);
request.onupgradeneeded = function(){
const db = request.result;
const store = db.createObjectStore("bots",{ keyPath: "id"});
}
request.onsuccess = function(){
document.write("database opened successfully");
const db = request.result;
const transaction=db.transaction("bots","readwrite");
const store = transaction.objectStore("bots");
store.add({id: 1, name: "jason",branch: "IT"});
store.add({id: 2, name: "praneeth",branch: "CSE"});
store.add({id: 3, name: "palli",branch: "EEE"});
store.add({id: 4, name: "abdul",branch: "IT"});
store.put({id: 4, name: "deevana",branch: "CSE"});
const idquery = store.get(4);
idquery.onsuccess = function(){
document.write("idquery",idquery.result);
}
transaction.oncomplete = function(){
db.close;
}
}
</script>
</body>
</html>
输出
database opened successfully
idquery {id: 4, name: 'deevana', branch: 'CSE'}
Previously the data stored in id: 4 was
Name: abdul Branch : IT
But as we updated the entry the values are changed.
广告