前面写过一篇:本地存储和数据库的文章,没基础的先看一下这里。不然你看着会懵。
还是回到我出文章的思路来
关于索引和游标,我会问
1、索引的API有哪些?
2、索引应该怎么使用?有哪些好处?
3、游标的API有哪些?
4、游标怎么使用?有哪些用途?
5、两位可以一起使用吗?
第一个问题:索引的API有哪些?
IDBObjectStore.createIndex() --------创建索引
该方法接收三个参数:
indexName ----索引名称
keyPath ---- 索引字段,可以为空或者数组
optionsParameters ---- 索引配置参数
关于optionsParameters的配置参数有:
1、unique表示keyPath字段的数据是否是唯一的
2、multiEntry表示是否为keyPath字段的每一项建立一条索引数据。(有点懵没关系,下面会有演示)
第二个问题:索引应该怎么使用?有哪些好处?





使用创建索引的好处在于:
1、可以使用存储记录中的值作为索引(不用创建多余的数据,提高效率)
2、索引会自动更新
3、索引数据自动排序
第三个问题:游标的API有哪些?
第四个问题:怎么使用
IDBObjectStore/IDBIndex.openCursor() -----创建游标
该方法返回一个IDBRequest对象,这个对象的结果返回IDBCursorWithValue对象。
可选参数有两个,
第一个是range----顾名思义是取值范围的意思。
一般值的写法是:IDBKeyRange.bound(key1,key2)
IDBKeyRange.only(key)
第二个direction----数据排列方向的意思
direction的话取值有四个,两个比较常用的prev和next
演示一下




第五个问题:游标和索引的结合使用
1、更新数据



2、删除对应字段的数据


如有侵权,联系作者删
网友评论