主頁 > 知識(shí)庫 > HTML5本地存儲(chǔ)之IndexedDB

HTML5本地存儲(chǔ)之IndexedDB

熱門標(biāo)簽:硅谷的囚徒呼叫中心 網(wǎng)站建設(shè) 智能手機(jī) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 使用U盤裝系統(tǒng) 檢查注冊(cè)表項(xiàng) 阿里云 美圖手機(jī)

IndexedDB 是一種低級(jí)API,用于客戶端存儲(chǔ)大量結(jié)構(gòu)化數(shù)據(jù)(包括, 文件/ blobs)。該API使用索引來實(shí)現(xiàn)對(duì)該數(shù)據(jù)的高性能搜索。

最近有一項(xiàng)業(yè)務(wù)需求,就是可以離線存儲(chǔ)數(shù)據(jù),等到有網(wǎng)絡(luò)信號(hào)的時(shí)候可以上傳表單和圖片。所以研究了一下HTML5的IndexedDB。

對(duì)于只存儲(chǔ)某些字段的需求來說,可以使用Local Storage和 Session Storage來完成。但是一旦存儲(chǔ)大量的數(shù)據(jù),Local Storage和 Session Storage就遠(yuǎn)遠(yuǎn)不能滿足需求了。這時(shí),IndexedDB的強(qiáng)大之處就會(huì)體現(xiàn)出來了。

1、創(chuàng)建或者打開數(shù)據(jù)庫

/* 對(duì)不同瀏覽器的indexedDB進(jìn)行兼容 */
const indexeddb = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
/* 創(chuàng)建或連接數(shù)據(jù)庫 */
const request = indexeddb.open(name, version);  // name:數(shù)據(jù)庫名,version:數(shù)據(jù)庫版本號(hào)

因?yàn)閕ndexedDB在不同的瀏覽器上有兼容性,所以我們需要些一個(gè)兼容函數(shù)來兼容indexedDB。

2、連接到數(shù)據(jù)庫的回調(diào)函數(shù)

request.addEventListener('success', function(event){ 
    // 打開或創(chuàng)建數(shù)據(jù)庫成功
}, false);
request.addEventListener('error', function(event){ 
    // 打開或創(chuàng)建數(shù)據(jù)庫失敗
}, false);
request.addEventListener('upgradeneeded', function(event){ 
    // 更新數(shù)據(jù)庫時(shí)執(zhí)行
}, false);

在連接到數(shù)據(jù)庫后,request會(huì)監(jiān)聽三種狀態(tài):

  • success:打開或創(chuàng)建數(shù)據(jù)庫成功
  • error:打開或創(chuàng)建數(shù)據(jù)庫失敗
  • upgradeneeded:更新數(shù)據(jù)庫

upgradeneeded狀態(tài)是在indexedDB創(chuàng)建新的數(shù)據(jù)庫時(shí)和indexeddb.open(name, version) version(數(shù)據(jù)庫版本號(hào))發(fā)生變化時(shí)才能監(jiān)聽到此狀態(tài)。當(dāng)版本號(hào)不發(fā)生變化時(shí),不會(huì)觸發(fā)此狀態(tài)。數(shù)據(jù)庫的ObjectStore的創(chuàng)建、刪除等都是在這個(gè)監(jiān)聽事件下執(zhí)行的。

3、創(chuàng)建、刪除ObjectStore

在indexedDB中,ObjectStore類似于數(shù)據(jù)庫的表。

request.addEventListener('upgradeneeded', function(event){ 
    // 創(chuàng)建數(shù)據(jù)庫實(shí)例
    const db = event.target.result;
    // 關(guān)閉數(shù)據(jù)庫
    db.close();
    // 判斷是否有ObjectStore
    db.objectStoreNames.contains(objectStoreName);
    // 刪除ObjectStore
    db.deleteObjectStore(objectStoreName);
}, false);

可以用如下方法創(chuàng)建一個(gè)ObjectStore

request.addEventListener('upgradeneeded', function(event){ 
    // 創(chuàng)建數(shù)據(jù)庫實(shí)例
    const db = event.target.result;
    // 判斷是否有ObjectStore
    if(!db.objectStoreNames.contains(objectStoreName)){
        const store = db.createObjectStore(objectStoreName, {
            keyPath: keyPath  // keyPath 作為ObjectStore的搜索關(guān)鍵字
        });
        // 為ObjectStore創(chuàng)造索引
        store.createIndex(name,    // 索引
                          index,   // 鍵值
                          {
                              unique: unique  // 索引是否唯一
                          });
    }
}, false);

4、數(shù)據(jù)的增刪改查

request.addEventListener('success', function(event){ 
    // 創(chuàng)建數(shù)據(jù)庫實(shí)例
    const db = event.target.result;
    // 查找一個(gè)ObjectStore
    db.transaction(objectStoreName, wa);
    // wa為'readwrite'時(shí),數(shù)據(jù)可以讀寫 
    // wa為'readonly'時(shí),數(shù)據(jù)只讀
    const store = transaction.objectStore(objectStoreName);
}, false);

數(shù)據(jù)庫的增刪改查:

// 添加數(shù)據(jù),當(dāng)關(guān)鍵字存在時(shí)數(shù)據(jù)不會(huì)添加
store.add(obj);
// 更新數(shù)據(jù),當(dāng)關(guān)鍵字存在時(shí)覆蓋數(shù)據(jù),不存在時(shí)會(huì)添加數(shù)據(jù)
store.put(obj);
// 刪除數(shù)據(jù),刪除指定的關(guān)鍵字對(duì)應(yīng)的數(shù)據(jù)
store.delete(value);
// 清除ObjectStore
store.clear();
// 查找數(shù)據(jù),根據(jù)關(guān)鍵字查找指定的數(shù)據(jù)
const g = store.get(value);
g.addEventListener('success', function(event){
    // 異步查找后的回調(diào)函數(shù)
}, false);

按索引查找數(shù)據(jù)

const index = store.index(indexName);
const cursor = index.openCursor(range);
cursor.addEventListener('success', function(event){
    const result = event.target.result;
    if(result){
        result.value       // 數(shù)據(jù)
        result.continue(); // 迭代,游標(biāo)下移
    }
}, false);

按索引的范圍查找數(shù)據(jù)

const index = store.index(indexName);
const cursor = index.openCursor(range);
/**
 * range為null時(shí),查找所有數(shù)據(jù)
 * range為指定值時(shí),查找索引滿足該條件的對(duì)應(yīng)的數(shù)據(jù)
 * range為IDBKeyRange對(duì)象時(shí),根據(jù)條件查找滿足條件的指定范圍的數(shù)據(jù)
 */
// 大于或大于等于 
range = IDBKeyRange.lowerBound(value, true)   // (value, +∞),>  value
range = IDBKeyRange.lowerBound(value, false)  // [value, +∞),>= value
// 小于或小于等于,isOpen:true,開區(qū)間;false,閉區(qū)間
range = IDBKeyRange.upperBound(value, isOpen)
// 大于或大于等于value1,小于或小于等于value2
IDBKeyRange.bound(value1, value2, isOpen1, isOpen2)

最后,自己封裝了一個(gè)indexedDB的庫,可以參考一下:duan602728596/IndexedDB

以上所述是小編給大家介紹的HTML5本地存儲(chǔ)之IndexedDB,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

標(biāo)簽:通遼 賀州 黃山 湘潭 湖北 山南 懷化 煙臺(tái)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5本地存儲(chǔ)之IndexedDB》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266