- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与演变
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - 内嵌框架 (iframe)
- HTML - 短语元素
- HTML - 元标签
- HTML - 类
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表头和标题
- HTML - 表格样式
- HTML - 表格列组 (colgroup)
- HTML - 嵌套表格
- HTML 列表
- HTML - 列表
- HTML - 无序列表
- HTML - 有序列表
- HTML - 定义列表
- HTML 链接
- HTML - 文本链接
- HTML - 图片链接
- HTML - 邮件链接
- HTML 颜色名称和值
- HTML - 颜色名称
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表单
- HTML - 表单
- HTML - 表单属性
- HTML - 表单控件
- HTML - 输入属性
- HTML 多媒体
- HTML - 视频元素
- HTML - 音频元素
- HTML - 嵌入多媒体
- HTML 头部
- HTML - head 元素
- HTML - 添加 Favicon
- HTML - JavaScript
- HTML 布局
- HTML - 布局
- HTML - 布局元素
- HTML - 使用 CSS 进行布局
- HTML - 响应式设计
- HTML - 符号
- HTML - 表情符号
- HTML - 样式指南
- HTML 图形
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 存储
- HTML - 服务器发送事件
- HTML 其他
- HTML - 文档对象模型 (DOM)
- HTML - MathML
- HTML - 微数据
- HTML - IndexedDB
- HTML - Web 消息传递
- HTML - Web CORS
- HTML - WebRTC
- HTML 演示
- HTML - 音频播放器
- HTML - 视频播放器
- HTML - 网页幻灯片
- HTML 工具
- HTML - Velocity 绘图
- HTML - 二维码
- HTML - Modernizr
- HTML - 验证
- HTML - 颜色拾取器
- HTML 参考
- HTML - 速查表
- HTML - 标签参考
- HTML - 属性参考
- HTML - 事件参考
- HTML - 字体参考
- HTML - ASCII 码
- ASCII 码表查找
- HTML - 颜色名称
- HTML - 实体
- MIME 媒体类型
- HTML - URL 编码
- 语言 ISO 代码
- HTML - 字符编码
- HTML - 已弃用的标签
- HTML 资源
- HTML - 快速指南
- HTML - 有用资源
- HTML - 颜色代码生成器
- HTML - 在线编辑器
HTML - IndexedDB
IndexedDB 是一个新的 HTML5 概念,用于将数据存储在用户的浏览器中。它比本地存储更强大,对于需要存储大量数据的应用程序非常有用。这些应用程序可以运行更高效并加载更快。
为什么要使用 IndexedDB?
W3C 已宣布 Web SQL 数据库是一个已弃用的本地存储规范,因此 Web 开发人员不应再使用此技术。IndexedDB 是 Web SQL 数据库的替代方案,并且比旧技术更有效。
特性
它存储键值对
它不是关系数据库
IndexedDB API 主要为异步的
它不是结构化查询语言
它允许访问同一域的数据
IndexedDB
在进入 IndexedDB 之前,我们需要添加一些实现前缀,如下所示:
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange if (!window.indexedDB) { window.alert("Your browser doesn't support a stable version of IndexedDB.") }
打开 IndexedDB 数据库
在创建数据库之前,我们必须为数据库准备一些数据。让我们从公司员工详细信息开始。
const employeeData = [ { id: "01", name: "Gopal K Varma", age: 35, email: "[email protected]" }, { id: "02", name: "Prasad", age: 24, email: "[email protected]" } ];
添加数据
这里手动向数据库中添加一些数据,如下所示:
function add() { var request = db.transaction(["employee"], "readwrite") .objectStore("employee") .add({ id: "01", name: "prasad", age: 24, email: "[email protected]" }); request.onsuccess = function(event) { alert("Prasad has been added to your database."); }; request.onerror = function(event) { alert("Unable to add data\r\nPrasad is already exist in your database! "); } }
检索数据
我们可以使用 get() 方法从数据库中检索数据。
function read() { var transaction = db.transaction(["employee"]); var objectStore = transaction.objectStore("employee"); var request = objectStore.get("00-03"); request.onerror = function(event) { alert("Unable to retrieve daa from database!"); }; request.onsuccess = function(event) { if(request.result) { alert("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email); } else { alert("Kenny couldn't be found in your database!"); } }; }
使用 get(),我们可以将数据存储在对象中,也可以将数据存储在游标中,然后从游标中检索数据。
function readAll() { var objectStore = db.transaction("employee").objectStore("employee"); objectStore.openCursor().onsuccess = function(event) { var cursor = event.target.result; if (cursor) { alert("Name for id " + cursor.key + " is " + cursor.value.name + ", Age: " + cursor.value.age + ", Email: " + cursor.value.email); cursor.continue(); } else { alert("No more entries!"); } }; }
删除数据
我们可以使用 remove() 方法从 IndexedDB 中删除数据。代码如下:
function remove() { var request = db.transaction(["employee"], "readwrite") .objectStore("employee") .delete("02"); request.onsuccess = function(event) { alert("prasad entry has been removed from your database."); }; }
HTML 代码
要显示所有数据,我们需要使用 onClick 事件,如下面的代码所示:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>IndexedDb Demo | onlyWebPro.com</title> </head> <body> <button onclick="read()">Read </button> <button onclick="readAll()"></button> <button onclick="add()"></button> <button onclick="remove()">Delete </button> </body> </html>
最终代码应如下所示:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> //prefixes of implementation that we want to test window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; //prefixes of window.IDB objects window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange if (!window.indexedDB) { window.alert("Your browser doesn't support a stable version of IndexedDB.") } const employeeData = [ { id: "00-01", name: "gopal", age: 35, email: "[email protected]" }, { id: "00-02", name: "prasad", age: 32, email: "[email protected]" } ]; var db; var request = window.indexedDB.open("newDatabase", 1); request.onerror = function(event) { console.log("error: "); }; request.onsuccess = function(event) { db = request.result; console.log("success: "+ db); }; request.onupgradeneeded = function(event) { var db = event.target.result; var objectStore = db.createObjectStore("employee", {keyPath: "id"}); for (var i in employeeData) { objectStore.add(employeeData[i]); } } function read() { var transaction = db.transaction(["employee"]); var objectStore = transaction.objectStore("employee"); var request = objectStore.get("00-03"); request.onerror = function(event) { alert("Unable to retrieve daa from database!"); }; request.onsuccess = function(event) { // Do something with the request.result! if(request.result) { alert("Name: " + request.result.name + ", Age: " + request.result.age + ", Email: " + request.result.email); } else { alert("Kenny couldn't be found in your database!"); } }; } function readAll() { var objectStore = db.transaction("employee").objectStore("employee"); objectStore.openCursor().onsuccess = function(event) { var cursor = event.target.result; if (cursor) { alert("Name for id " + cursor.key + " is " + cursor.value.name + ", Age: " + cursor.value.age + ", Email: " + cursor.value.email); cursor.continue(); } else { alert("No more entries!"); } }; } function add() { var request = db.transaction(["employee"], "readwrite") .objectStore("employee") .add({ id: "00-03", name: "Kenny", age: 19, email: "[email protected]" }); request.onsuccess = function(event) { alert("Kenny has been added to your database."); }; request.onerror = function(event) { alert("Unable to add data\r\nKenny is aready exist in your database! "); } } function remove() { var request = db.transaction(["employee"], "readwrite") .objectStore("employee") .delete("00-03"); request.onsuccess = function(event) { alert("Kenny's entry has been removed from your database."); }; } </script> </head> <body> <button onclick="read()">Read </button> <button onclick="readAll()">Read all </button> <button onclick="add()">Add data </button> <button onclick="remove()">Delete data </button> </body> </html>
广告