博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTML5本地存储——IndexedDB二:索引
阅读量:4690 次
发布时间:2019-06-09

本文共 3267 字,大约阅读时间需要 10 分钟。

 

在中介绍了关于IndexedDB的基本使用方法,很不过瘾,这篇我们来看看indexedDB的杀器——索引。

熟悉数据库的同学都知道索引的一个好处就是可以迅速定位数据,提高搜索速度,在indexedDB中有两种索引,一种是自增长的int值,一种是keyPath:自己指定索引列,我们重点来看看keyPath方式的索引使用.

创建索引

我们可以在创建object store的时候指明索引,使用object store的createIndex创建索引,方法有三个参数

  • 索引名称
  • 索引属性字段名
  • 索引属性值是否唯一
function openDB (name,version) {            var version=version || 1;            var request=window.indexedDB.open(name,version); request.οnerrοr=function(e){ console.log(e.currentTarget.error.message); }; request.onsuccess=function(e){ myDB.db=e.target.result; }; request.onupgradeneeded=function(e){ var db=e.target.result; if(!db.objectStoreNames.contains('students')){ var store=db.createObjectStore('students',{keyPath: 'id'}); store.createIndex('nameIndex','name',{unique:true}); store.createIndex('ageIndex','age',{unique:false}); } console.log('DB version changed to '+version); }; }

这样我们在students 上创建了两个索引

 

利用索引获取数据

function getDataByIndex(db,storeName){            var transaction=db.transaction(storeName);            var store=transaction.objectStore(storeName); var index = store.index("nameIndex"); index.get('Byron').onsuccess=function(e){ var student=e.target.result; console.log(student.id); } }

这样我们可以利用索引快速获取数据,name的索引是唯一的没问题,但是对于age索引只会取到第一个匹配值,要想得到所有age符合条件的值就需要使用游标了

游标

在indexedDB中使用索引和游标是分不开的,对数据库熟悉的同学很好理解游标是什么东东,有了数据库object store的游标,我们就可以利用游标遍历object store了。

使用object store的openCursor()方法打开游标

function fetchStoreByCursor(db,storeName){            var transaction=db.transaction(storeName);            var store=transaction.objectStore(storeName); var request=store.openCursor(); request.onsuccess=function(e){ var cursor=e.target.result; if(cursor){ console.log(cursor.key); var currentStudent=cursor.value; console.log(currentStudent.name); cursor.continue(); } }; }

curson.contine()会使游标下移,知道没有数据返回undefined

index与游标结合

 

要想获取age为26的student,可以结合游标使用索引

function getMultipleData(db,storeName){            var transaction=db.transaction(storeName);            var store=transaction.objectStore(storeName); var index = store.index("ageIndex"); var request=index.openCursor(IDBKeyRange.only(26)) request.onsuccess=function(e){ var cursor=e.target.result; if(cursor){ var student=cursor.value; console.log(student.id); cursor.continue(); } } }

这样我们可是使用索引打开一个游标,参数下面会讲到,在成功的句柄内获得游标便利age为26的student,也可以通过index.openKeyCursor()方法只获取每个对象的key值。

指定游标范围

index.openCursor()/index.openKeyCursor()方法在不传递参数的时候会获取object store所有记录,像上面例子一样我们可以对搜索进行筛选
可以使用key range 限制游标中值的范围,把它作为第一个参数传给 openCursor() 或是 openKeyCursor()
IDBKeyRange.only(value):只获取指定数据
IDBKeyRange.lowerBound(value,isOpen):获取最小是value的数据,第二个参数用来指示是否排除value值本身,也就是数学中的是否是开区间
IDBKeyRange.upperBound(value,isOpen):和上面类似,用于获取最大值是value的数据
IDBKeyRange.bound(value1,value2,isOpen1,isOpen2):不用解释了吧

 

获取名字首字母在B-E的student

function getMultipleData(db,storeName){            var transaction=db.transaction(storeName);            var store=transaction.objectStore(storeName); var index = store.index("nameIndex"); var request=index.openCursor(IDBKeyRange.bound('B','F',false,
true
));            request.onsuccess=function(e){                var cursor=e.target.result;                if(cursor){ var student=cursor.value; console.log(student.name); cursor.continue(); } } }

 完整示例

 

1   2  3  4 IndexedDB 5  6  7 

转载于:https://www.cnblogs.com/benbentu/p/4930106.html

你可能感兴趣的文章
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>
C#和JAVA 访问修饰符
查看>>
集合框架
查看>>
小甲鱼OD学习第1讲
查看>>
【转】简述生成式对抗网络
查看>>
HDU-1085 Holding Bin-Laden Captive-母函数
查看>>
php提示undefined index的几种解决方法
查看>>
轻量级原生 ajax 函数,支持 get/array post/array post/json
查看>>
LRJ
查看>>
Struts2环境搭建
查看>>
Linux: Check version info
查看>>
Javascript-正则表达式-开发中的使用.
查看>>