IndexedDB入门

news/2025/2/19 16:30:47/

https://www.cnblogs.com/zhangzuwei/p/16574791.html

注意

1.删除表,创建表只能在数据库版本升级里面进行。

2.keyPath: key 要和表字段对应,而且格式要一样,不然不运行不报错。

3.使用 autoIncrement: true 代替 keyPath: key, 则不需要写关键字段。

<html>
<head>
<title>IndexedDB</title>
<style>
h1 { text-align:center; }
table { margin:auto; border-collapse:collapse; }
th, td { text-align:center; padding:10px; border:1px solid black; }
</style>
</head>
<body>
<h1>Customers</h1>
<p id="msg"></p>
<table id="table"></table>
<script>
if (!window.indexedDB) {msg.innerText = "Your browser doesn't support IndexedDB.";
} else {const customerData = [{ name: "Bill", age: 35, email: "bill@company.com" },{ name: "Donna", age: 32, email: "donna@home.org" },{ name: "Jenny", age: 23, email: "jenny@msn.com" },{ name: "Henry", age: 43, email: "Henry@outlook.com" },{ name: "Kaili", age: 53, email: "Kaili@outlook.com" }];var db;var request = window.indexedDB.open("MyTestDatabase", 1);request.onerror = function(event) {console.log(event);msg.innerText = event.target.error;};request.onupgradeneeded = function(event){db = event.target.result;if (db.objectStoreNames.contains('customers')){db.deleteObjectStore("customers");}var objectStore = db.createObjectStore("customers", { autoIncrement: true });objectStore.createIndex("name", "name", { unique: false });objectStore.createIndex("email", "email", { unique: true }); objectStore.transaction.oncomplete = function(event){var customerObjectStore = db.transaction("customers", "readwrite").objectStore("customers");customerData.forEach(function(customer){customerObjectStore.add(customer);               });};};request.onsuccess = function(event){        var i = 1;var tr = document.createElement('tr');var th = document.createElement('th');th.textContent = 'id';tr.append(th);th = document.createElement('th');th.textContent = 'name';tr.append(th);th = document.createElement('th');th.textContent = 'age';tr.append(th);th = document.createElement('th');th.textContent = 'email';tr.append(th);table.append(tr);db = event.target.result;var objectStore = db.transaction("customers").objectStore("customers");        objectStore.openCursor().onsuccess = function(event){var cursor = event.target.result;            if (cursor) {tr = document.createElement('tr');var td = document.createElement('td');td.textContent = i;tr.append(td);td = document.createElement('td');td.textContent = cursor.value.name;tr.append(td);td = document.createElement('td');td.textContent = cursor.value.age;tr.append(td);td = document.createElement('td');td.textContent = cursor.value.email;tr.append(td);table.append(tr);cursor.continue();                i++;} else {console.log("No more cursor!");}};};    }
</script>
</body>
</html>


http://www.ppmy.cn/news/1336046.html

相关文章

基于springboot网上图书商城源码和论文

在Internet高速发展的今天&#xff0c;我们生活的各个领域都涉及到计算机的应用&#xff0c;其中包括网上图书商城的网络应用&#xff0c;在外国网上图书商城已经是很普遍的方式&#xff0c;不过国内的管理网站可能还处于起步阶段。网上图书商城具有网上图书信息管理功能的选择…

ctfshow-命令执行

大佬文章 u r l \rm url url 编码 L i n u x \rm Linux Linux 下空格绕过 无参数 r c e \rm rce rce 无字母 r c e \rm rce rce 无字母参数 r c e ( w e b 55 ) \rm rce(web55) rce(web55) web29 通配符&#xff1a; *&#xff1a;匹配任意多个字符 ?&#xff1a;匹配任…

【每日一题】5.LeetCode——环形链表

&#x1f4da;博客主页&#xff1a;爱敲代码的小杨. ✨专栏&#xff1a;《Java SE语法》 ❤️感谢大家点赞&#x1f44d;&#x1f3fb;收藏⭐评论✍&#x1f3fb;&#xff0c;您的三连就是我持续更新的动力❤️ &#x1f64f;小杨水平有限&#xff0c;欢迎各位大佬指点&…

网络安全面试宝典——黑客渗透

#在面试时&#xff0c;网络安全也会被经常问到&#xff0c;至少要知道常见的攻击&#xff0c;以及防 御措施。在这里 Mark 下&#xff0c;不做深入分析。 1.对称加密和非对称加密 对称加密&#xff1a;加解密用同一密钥&#xff0c;密钥维护复杂 n&#xff08;n-1&#xff…

【Vue】为什么Vue3使用Proxy代替defineProperty?

先来看看 Vue2 中 defineProperty 来操作数据&#xff1a; const obj {a: 1,b: 2,c: {a: 1,b: 2} } function _isObject(v) {return typeof v object && v ! null; } function observe(object) {for (let key in object) {let v object[key];if (_isObject(v)) {ob…

Redis -- 背景知识

目录 特性 为啥Redis快? 应用场景 Redis不能做什么&#xff1f; Redis是在内存中存储数据的一个中间件&#xff0c;用作为数据库&#xff0c;也可以用作为缓存&#xff0c;在分布式中有很高的威望。 特性 In-memory data structures&#xff1a;在内存中存储数据key-val…

PMP五大过程组:项目成功的金钥匙

在项目管理领域&#xff0c;PMP&#xff08;项目管理专业&#xff09;一直被视为权威的标准。PMP认证的项目管理方法论&#xff0c;即五大过程组和十大知识领域&#xff0c;为项目管理提供了一套完整、系统的方法论。五大过程组作为PMP的核心&#xff0c;涵盖了项目从开始到结束…

C++ STL中list迭代器的实现

list 的模拟实现中&#xff0c;重难点在于迭代器功能的实现&#xff0c;因此本文只围绕 iterator 及 const_iterator 的设计进行介绍&#xff0c;其余如增删查改则不再赘述——在C语言的基础上&#xff0c;这些都非常简单。 与 string / vector 不同&#xff0c;list 的节点原生…