indexDB数据库
参考网址:https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API
该数据库是一种存储在客户端本笃的NoSQL数据库,他是事务性数据库系统
IndexedDB里数据以对象的形式存储,每个对象都有一个key值索引。IndexedDB里的操作都是事务性的。一种对象存储在一个objectStore里,objectStore就相当于关系数据库里的表。IndexedDB可以有很多objectStore,objectStore里可以有很多对象。每个对象可以用key值获取。
这是在各个浏览器中都能统一运行
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;
window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor;
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
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;
window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor;
function versionUpdata(){
var dbName = "indexedDBTest";//数据库库名
var dbVersion = 1.1;//版本号
var idb;
var dbConnect = indexedDB.open(dbName,dbVersion);
//dbConnect对象为一个IDBOpenDBRequest对象,代表数据库链接的请求对象
dbConnect.onsuccess = function(e){
idb = e.target.result;//e.target.result对象为一个IDBDBRequest对象,代表链接成功的数据对象
console.log("e.target.result ::"+e.target.result);
}
dbConnect.onerror = function(e){
console.log("链接数据库失败");
}
dbConnect.onupgradeneeded = function(e){//onupgradeneeded链接数据库是发现指定的版本号大于数据库当前版本号触发的事件
idb = e.target.result;
var tx = e.target.transaction;//数据版本更新事物
/*@检测版本号的
var oldVersion = e.oldVersion;
var newVersion = e.newVersion;
console.log("更新成功,老版本" +oldVersion ,"新版本" + newVersion);*/
/*@创建仓库
var name = 'Users';
var optionalParameters = {
keyPath : 'UserId',
autoIncrement : false
}
var store = idb.createObjectStore(name , optionalParameters);
console.log("创建仓库成功");*/
}
}
</script>
</head>
<body>
<input type="button" value="链接数据库" onclick="versionUpdata()"/>
</body>
</html>
创建indexedDB数据库中的索引
需要通过数据记录对象的某个属性值来创建,他只能针对被设为索引的属性值进行检索,不能针对没有被设为索引的属性值进行检索
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
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;
window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor;
function CreateIndex(){
var dbName = "indexedDBTest";//数据库库名
var dbVersion = 1.1;//版本号
var idb;
var dbConnect = indexedDB.open(dbName,dbVersion);
//dbConnect对象为一个IDBOpenDBRequest对象,代表数据库链接的请求对象
dbConnect.onsuccess = function(e){
idb = e.target.result;//e.target.result对象为一个IDBDBRequest对象,代表链接成功的数据对象
console.log("e.target.result ::"+e.target.result);
}
dbConnect.onerror = function(){
console.log("链接数据库失败");
}
dbConnect.onupgradeneeded = function(e){//onupgradeneeded是我们唯一可以修改数据库结构的地方,在这里面,我们可以创建和删除对象存储空间以及构建和删除索引
debugger;
idb = e.target.result;
var tx = e.target.transaction;//数据版本更新事物
// @创建仓库
// 创建一个对象存储空间来持有有关我们客户的信息。
// 我们将使用 "UserId" 作为我们的 key path 因为它保证是唯一的。
var name = 'newUsers';
var optionalParameters = {
keyPath : 'UserId',
autoIncrement : false
}
//对象存储空间仅调用 createObjectStore() 就可以创建
var store = idb.createObjectStore(name , optionalParameters);
console.log("创建仓库成功");
/*
*再创建仓库成功之后,调用仓库的createIndex方法创建索引
* @createIndex有3个参数
* 1字符串,代表索引名
* 2参数值代表使用数据仓库中数据记录对象的那个属性来创建索引
* */
var name = 'userNameInter'; // 创建一个索引来通过 name 搜索客户。
var keyPath = 'useName';
var optionalParameters = {
unique:false, //可能会有重复的,因此我们不能使用 unique 索引。unique的属性值为true,代表同一个对象仓库的两条数据记录的索引属性值(useName)不能相同
multiEntry:false
}
var idx = store.createIndex(name,keyPath,optionalParameters);
console.log("索引创建成功");
}
}
</script>
</head>
<body>
<input type="button" value="创建索引" onclick="CreateIndex()"/>
</body>
</html>