indexedDB(三)查询(详细)篇
前言
上篇講了簡單的創(chuàng)建了一個數(shù)據(jù)庫和兩個表,這里就詳細(xì)記錄下查詢;
這里先定義二個數(shù)組對象
data(){return {arr:[{id:1,name:'楊姐',age:18,sex:0},{id:2,name:'駿馳',age:33,sex:0},{id:3,name:'king',age:45,sex:1},{id:4,name:'浩杰',age:16,sex:0},{id:5,name:'浩B',age:89,sex:1},{id:6,name:'浩A',age:56,sex:0},],arr1:[{id:1,height:160,weight:50},{id:2,height:150,weight:45},{id:3,height:160,weight:70},{id:4,height:170,weight:70},{id:5,height:180,weight:75},{id:6,height:180,weight:60},],db:null, // 全局的indexedDB數(shù)據(jù)庫實例。}},上面的db 在初始化數(shù)據(jù)庫的時候 賦值了 this.db=e.target.result;
表一 yyyyy 鍵值id 索引就是里面的3個字段
表二 two 鍵值id 索引就是里面的3個字段
根據(jù)鍵值查詢
/ 讀取數(shù)據(jù)getData(){let {db} = this;// 創(chuàng)建一個事務(wù)var request = db.transaction(['yyyyy'], 'readwrite').objectStore('yyyyy');let myObj = request.get(5);myObj.onsuccess = function (event) {console.log('數(shù)據(jù)讀取成功',event.target.result);};myObj.onerror = function (event) {console.log('數(shù)據(jù)讀取失敗');}},根據(jù)索引查詢
注:索引是可以不唯一出現(xiàn)多個的,例如年齡,你存的數(shù)據(jù)里可以出現(xiàn)多個年齡一樣的人,返回結(jié)果是第一個人
/ 讀取數(shù)據(jù)getData(){let {db} = this;// 創(chuàng)建一個事務(wù)var request = db.transaction(['yyyyy'], 'readwrite').objectStore('yyyyy');// 單索引let result = request.index('sex');let myObj = request.get(0);myObj.onsuccess = function (event) {console.log('數(shù)據(jù)讀取成功',event.target.result);};myObj.onerror = function (event) {console.log('數(shù)據(jù)讀取失敗');}},索引游標(biāo)查詢,數(shù)據(jù)遍歷
注:IDBKeyRange.only(1) 返回sex===1的
IDBKeyRange.lowerBound():指定下限。
IDBKeyRange.upperBound():指定上限。
IDBKeyRange.bound():同時指定上下限。
IDBKeyRange.only():指定只包含一個值。
// All keys ≤ x
var r1 = IDBKeyRange.upperBound(x);
// All keys < x
var r2 = IDBKeyRange.upperBound(x, true);
// All keys ≥ y
var r3 = IDBKeyRange.lowerBound(y);
// All keys > y
var r4 = IDBKeyRange.lowerBound(y, true);
// All keys ≥ x && ≤ y
var r5 = IDBKeyRange.bound(x, y);
// All keys > x &&< y
var r6 = IDBKeyRange.bound(x, y, true, true);
// All keys > x && ≤ y
var r7 = IDBKeyRange.bound(x, y, true, false);
// All keys ≥ x &&< y
var r8 = IDBKeyRange.bound(x, y, false, true);
// The key = z
var r9 = IDBKeyRange.only(z);
游標(biāo)查詢,數(shù)據(jù)遍歷 (2)
如果條件比較多可以在onsuccess里面去判斷,判斷沒有的話就是獲取所有
let c = result.openCursor(); //條件查詢c.onsuccess = function(e) {//成功執(zhí)行回調(diào)var cursor = e.target.result;if (cursor){//如果存在if(cursor.key==1){newArr.push(cursor)} cursor.continue();//繼續(xù)下一個}}},多表關(guān)聯(lián)查詢
功能 需查找 sex為0的人 身高高于160的 manyGet()
// 多表查詢async manyGet(){// 需查找 sex為0的人 身高高于160的let n = await this.getYYYY();// 再啟一個事務(wù) 查詢two表let newArr = [];await Promise.all(n.map(async (item,index) => {let two = await this.getTwo(item.id);if(two){Object.assign(item,two);newArr.push(item)}}))console.log(newArr);},getYYYY(){return new Promise((resolve,reject)=>{let {db} = this;// 創(chuàng)建一個事務(wù)var request = db.transaction(['yyyyy'], 'readwrite').objectStore('yyyyy');// 單索引let result = request.index('sex');let newArr = []// 打開游標(biāo)let c = result.openCursor(IDBKeyRange.only(0));c.onsuccess = async (e) => {//成功執(zhí)行回調(diào)console.log('成功執(zhí)行回調(diào)');var cursor = e.target.result;if (cursor){//如果存在// console.log(cursor.key);//key是表的主鍵var stu = cursor.value;newArr.push(stu)cursor.continue();//繼續(xù)下一個}else {resolve(newArr)}}})},getTwo(id){return new Promise((resolve,reject)=>{let {db} = this;var mmm = db.transaction(['two'], 'readwrite').objectStore('two').get(id);mmm.onsuccess = function (event) {// console.log('多表查詢',event);let item = event.target.result;console.log(1111);if(item.height>160){resolve(item)}else{resolve()}};})},總結(jié)
以上是生活随笔為你收集整理的indexedDB(三)查询(详细)篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器人运动估计——IMU运动方程与ESK
- 下一篇: 批量修改文件夹下文件的名字