IndexDB的出现 是为了存储更大量的结构化数据前端
demo地址git
IndexedDB是一个事务型数据库系统,相似于基于SQL的RDBMS。 然而不一样的是它使用固定列表(只有 key,value),IndexedDB是一个基于JavaScript的面向对象的数据库。
IndexedDB容许您存储和检索用键索引的对象; 能够存储structured clone algorithm支持的任何对象。
您只须要指定数据库模式,打开与数据库的链接,而后检索和更新一系列事务中的数据。github
IndexedDB 是非关系型数据库sql
WebSql 是关系型数据库, 前端须要写sql ,目前 WebSql已经浏览器基本已经放弃数据库
1.打开数据库而且开始一个事务。
2.建立一个 object store。
3.构建一个请求来执行一些数据库操做,像增长或提取数据等。
4.经过监听正确类型的 DOM 事件以等待操做完成。
5.在操做结果上进行一些操做(能够在 request 对象中找到)浏览器
var dbName = "the_name"; var db ; // 链接数据库,没有就建立数据库 var request = indexedDB.open(dbName, 2); request.onsuccess = function(event){ db = request.result; } // 错误处理程序在这里。 request.onerror = function(event) { console.log(event); }; request.onupgradeneeded = function(event) { db = event.target.result; // 建立一个对象存储空间来, 自增主键 var objectStore = db.createObjectStore("customers"); }; // 添加数据 var customerData = [ { id:"1", name: "Bill", age: 35, email: "bill@company.com" }, { id:"2", name: "Donna", age: 32, email: "donna@home.org" } ]; var transaction = db.transaction(dbName, "readwrite"); // 打开存储对象 var objectStore = transaction.objectStore('customers'); // 添加到数据对象中 customerData.forEach(function(item){ objectStore.put(item,item.id); }); // 查询 db.transaction("customers").objectStore("customers").get("1").onsuccess = function(event) { console.log(event.target.result.name); };