久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      H5中indexedDB 數(shù)據(jù)庫的使用方法詳解

      本篇文章主要介紹了HTML5中indexedDB 數(shù)據(jù)庫的使用實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

      前言

      在 HTML5 的本地存儲(chǔ)中,有一種叫 indexedDB 的數(shù)據(jù)庫,該數(shù)據(jù)庫是一種存儲(chǔ)在客戶端本地的 NoSQL 數(shù)據(jù)庫,它可以存儲(chǔ)大量的數(shù)據(jù)。從上篇:HTML5 進(jìn)階系列:web Storage ,我們知道 web Storage 可以方便靈活的在本地存取簡(jiǎn)單數(shù)據(jù),但是對(duì)于大量結(jié)構(gòu)化存儲(chǔ),indexedDB 的優(yōu)勢(shì)就更加明顯。接下來我們來看看 indexedDB 如何存儲(chǔ)數(shù)據(jù)。

      連接數(shù)據(jù)庫

      一個(gè)網(wǎng)站可以有多個(gè) indexedDB 數(shù)據(jù)庫,但每個(gè)數(shù)據(jù)庫的名稱是唯一的。我們需要通過數(shù)據(jù)庫名來連接某個(gè)具體的數(shù)據(jù)庫。

      var request = indexedDB.open('dbName', 1);  // 打開 dbName 數(shù)據(jù)庫  request.onerror = function(e){              // 監(jiān)聽連接數(shù)據(jù)庫失敗時(shí)執(zhí)行      console.log('連接數(shù)據(jù)庫失敗');  }  request.onsuccess = function(e){            // 監(jiān)聽連接數(shù)據(jù)庫成功時(shí)執(zhí)行      console.log('連接數(shù)據(jù)庫成功');  }

      我們使用 indexedDB.open 方法來連接數(shù)據(jù)庫,該方法接收兩個(gè)參數(shù),第一個(gè)是數(shù)據(jù)庫名,第二個(gè)是數(shù)據(jù)庫版本號(hào)。該方法會(huì)返回一個(gè) IDBOpenDBRequest 對(duì)象,代表一個(gè)請(qǐng)求連接數(shù)據(jù)庫的請(qǐng)求對(duì)象。我們可以通過監(jiān)聽請(qǐng)求對(duì)象的 onsuccess 和 onerror 事件來定義連接成功或失敗需執(zhí)行的方法。

      因?yàn)?indexedDB API 中不允許數(shù)據(jù)庫中的數(shù)據(jù)倉庫在同一版本中發(fā)生變化,所以需要在 indexedDB.open 方法中傳入新的版本號(hào)來更新版本,避免在同一版本中重復(fù)修改數(shù)據(jù)庫。版本號(hào)必須為整數(shù)!

      var request = indexedDB.open('dbName', 2);  // 更新版本,打開版本為2的數(shù)據(jù)庫  // ...  request.onupgradeneeded = function(e){      console.log('新數(shù)據(jù)庫版本號(hào)為=' + e.newVersion);  }

      我們通過監(jiān)聽請(qǐng)求對(duì)象的 onupgradeneeded 事件來定義數(shù)據(jù)庫版本更新時(shí)執(zhí)行的方法。

      關(guān)閉數(shù)據(jù)庫

      使用 indexedDB.open 連接數(shù)據(jù)庫成功后會(huì)返回一個(gè) IDBOpenDBRequest 對(duì)象,我們可以調(diào)用該對(duì)象的 close 方法來關(guān)閉數(shù)據(jù)庫。

      var request = indexedDB.open('dbName', 2);  // ...  request.onsuccess = function(e){      console.log('連接數(shù)據(jù)庫成功');      var db = e.target.result;      db.close();      console.log('數(shù)據(jù)庫已關(guān)閉');  }

      刪除數(shù)據(jù)庫

      indexedDB.deleteDatabase('dbName');  console.log('數(shù)據(jù)庫已刪除');

      創(chuàng)建對(duì)象倉庫

      object store(對(duì)象倉庫)是 indexedDB 數(shù)據(jù)庫的基礎(chǔ),在indexedDB 中并沒有數(shù)據(jù)庫表,而對(duì)象倉庫,就是相當(dāng)于一個(gè)數(shù)據(jù)庫表。

      var request = indexedDB.open('dbName', 3);  // ...  request.onupgradeneeded = function(e){      var db = e.target.result;      var store = db.createObjectStore('Users', {keyPath: 'userId', autoIncrement: false});      console.log('創(chuàng)建對(duì)象倉庫成功');  }

      db.createObjectStore 方法接收兩個(gè)參數(shù),第一個(gè)為對(duì)象倉庫名,第二個(gè)參數(shù)為可選參數(shù),值為一個(gè)js對(duì)象。該對(duì)象中的 keyPath 屬性為主鍵,相當(dāng)于數(shù)據(jù)庫表中 id 為主鍵。autoIncrement 屬性為 false,則表示主鍵值不自增,添加數(shù)據(jù)時(shí)需指定主鍵值。

      注意:在數(shù)據(jù)庫中,對(duì)象倉庫名不可重復(fù),否則瀏覽器會(huì)報(bào)錯(cuò)。

      創(chuàng)建索引

      indexedDB 數(shù)據(jù)庫中通過數(shù)據(jù)對(duì)象的某個(gè)屬性來創(chuàng)建索引,在數(shù)據(jù)庫中進(jìn)行檢索時(shí),只能通過被設(shè)為索引的屬性進(jìn)行檢索。

      var request = indexedDB.open('dbName', 4);  // ...  request.onupgradeneeded = function(e){      var db = e.target.result;      var store = db.createObjectStore('newUsers', {keyPath: 'userId', autoIncrement: false});      var idx = store.createIndex('usernameIndex','userName',{unique: false})      console.log('創(chuàng)建索引成功');  }

      store.createIndex 方法接收三個(gè)參數(shù),第一個(gè)為索引名,第二個(gè)為數(shù)據(jù)對(duì)象的屬性,上例中使用 userName 屬性來創(chuàng)建索引,第三個(gè)參數(shù)為可選參數(shù),值為一個(gè)js對(duì)象。該對(duì)象中的 unique 屬性為 true,代表索引值不可以相同,即兩條數(shù)據(jù)的 userName 不可以相同,為 false 則可以相同。

      基于事務(wù)

      在 indexedDB 中,所有數(shù)據(jù)操作都只能在事務(wù)中執(zhí)行。連接數(shù)據(jù)庫成功后,可以使用 IDBOpenDBRequest 對(duì)象的 transaction 方法開啟只讀事務(wù)或讀寫事務(wù)。

      var request = indexedDB.open('dbName', 5);  // ...  request.onupgradeneeded = function(e){      var db = e.target.result;      var tx = db.transaction('Users','readonly');      tx.oncomplete = function(e){          console.log('事務(wù)結(jié)束了');      }      tx.onabort = function(e){          console.log('事務(wù)被中止了');      }  }

      db.transaction 方法接收兩個(gè)參數(shù),第一個(gè)參數(shù)可以是字符串或數(shù)組,字符串時(shí)則是一個(gè)對(duì)象倉庫名,數(shù)組時(shí)則是由對(duì)象倉庫名組成的數(shù)組,transaction 可以對(duì)參數(shù)中任何一個(gè)對(duì)象倉庫進(jìn)行操作。第二個(gè)參數(shù)為事務(wù)模式,傳入 readonly 時(shí)只能對(duì)對(duì)象倉庫進(jìn)行讀操作,無法寫操作??梢詡魅?readwrite 進(jìn)行讀寫操作。

      操作數(shù)據(jù)

      1. add() : 增加數(shù)據(jù)。接收一個(gè)參數(shù),為需要保存到對(duì)象倉庫中的對(duì)象。

      2. put() : 增加或修改數(shù)據(jù)。接收一個(gè)參數(shù),為需要保存到對(duì)象倉庫中的對(duì)象。

      3. get() : 獲取數(shù)據(jù)。接收一個(gè)參數(shù),為需要獲取數(shù)據(jù)的主鍵值。

      4. delete() : 刪除數(shù)據(jù)。接收一個(gè)參數(shù),為需要獲取數(shù)據(jù)的主鍵值。

      var request = indexedDB.open('dbName', 5);  // ...  request.onsuccess = function(e){      var db = e.target.result;      var tx = db.transaction('Users','readwrite');      var store = tx.objectStore('Users');      var value = {          'userId': 1,          'userName': 'linxin',          'age': 24      }      var req1 = store.put(value);        // 保存數(shù)據(jù)      var req2 = store.get(1);            // 獲取索引userId為1的數(shù)據(jù)      req2.onsuccess = function(){          console.log(this.result.userName);  // linxin      }      var req3 = store.delete(1);             // 刪除索引為1的數(shù)據(jù)      req3.onsuccess = function(){          console.log('刪除數(shù)據(jù)成功');        // 刪除數(shù)據(jù)成功      }  }

      add 和 put 的作用類似,區(qū)別在于 put 保存數(shù)據(jù)時(shí),如果該數(shù)據(jù)的主鍵在數(shù)據(jù)庫中已經(jīng)有相同主鍵的時(shí)候,則會(huì)修改數(shù)據(jù)庫中對(duì)應(yīng)主鍵的對(duì)象,而使用 add 保存數(shù)據(jù),如果該主鍵已經(jīng)存在,則保存失敗。

      檢索數(shù)據(jù)

      上面我們知道使用 get() 方法可以獲取數(shù)據(jù),但是需要制定主鍵值。如果我們想要獲取一個(gè)區(qū)間的數(shù)據(jù),可以使用游標(biāo)。游標(biāo)通過對(duì)象倉庫的 openCursor 方法創(chuàng)建并打開。

      openCursor 方法接收兩個(gè)參數(shù),第一個(gè)是 IDBKeyRange 對(duì)象,該對(duì)象創(chuàng)建方法主要有以下幾種:

      // boundRange 表示主鍵值從1到10(包含1和10)的集合。  // 如果第三個(gè)參數(shù)為true,則表示不包含最小鍵值1,如果第四參數(shù)為true,則表示不包含最大鍵值10,默認(rèn)都為false  var boundRange = IDBKeyRange.bound(1, 10, false, false);    // onlyRange 表示由一個(gè)主鍵值的集合。only() 參數(shù)則為主鍵值,整數(shù)類型。  var onlyRange = IDBKeyRange.only(1);    // lowerRaneg 表示大于等于1的主鍵值的集合。  // 第二個(gè)參數(shù)可選,為true則表示不包含最小主鍵1,false則包含,默認(rèn)為false  var lowerRange = IDBKeyRange.lowerBound(1, false);    // upperRange 表示小于等于10的主鍵值的集合。  // 第二個(gè)參數(shù)可選,為true則表示不包含最大主鍵10,false則包含,默認(rèn)為false  var upperRange = IDBKeyRange.upperBound(10, false);

      openCursor 方法的第二個(gè)參數(shù)表示游標(biāo)的讀取方向,主要有以下幾種:

      1. next : 游標(biāo)中的數(shù)據(jù)按主鍵值升序排列,主鍵值相等的數(shù)據(jù)都被讀取

      2. nextunique : 游標(biāo)中的數(shù)據(jù)按主鍵值升序排列,主鍵值相等只讀取第一條數(shù)據(jù)

      3. prev : 游標(biāo)中的數(shù)據(jù)按主鍵值降序排列,主鍵值相等的數(shù)據(jù)都被讀取

      4. prevunique : 游標(biāo)中的數(shù)據(jù)按主鍵值降序排列,主鍵值相等只讀取第一條數(shù)據(jù)

      var request = indexedDB.open('dbName', 6);  // ...  request.onsuccess = function(e){      var db = e.target.result;      var tx = db.transaction('Users','readwrite');      var store = tx.objectStore('Users');      var range = IDBKeyRange.bound(1,10);      var req = store.openCursor(range, 'next');      req.onsuccess = function(){          var cursor = this.result;          if(cursor){              console.log(cursor.value.userName);              cursor.continue();          }else{              console.log('檢索結(jié)束');          }      }  }

      當(dāng)存在符合檢索條件的數(shù)據(jù)時(shí),可以通過 update 方法更新該數(shù)據(jù):

      cursor.updata({      userId : cursor.key,      userName : 'Hello',      age : 18  });

      可以通過 delete 方法刪除該數(shù)據(jù):

      cursor.delete();

      可以通過 continue 方法繼續(xù)讀取下一條數(shù)據(jù),否則讀到第一條數(shù)據(jù)之后不再繼續(xù)讀取:

      cursor.continue();

      總結(jié)

      從連接數(shù)據(jù)庫,創(chuàng)建對(duì)象倉庫、索引,到操作、檢索數(shù)據(jù),完成了 indexedDB 存取數(shù)據(jù)的完整流程。下面通過一個(gè)完整的例子來更好地掌握 indexedDB 數(shù)據(jù)庫。代碼地址:indexedDB-demo

      【相關(guān)推薦】

      1. Javacript免費(fèi)視頻教程

      2. 為什么現(xiàn)在HTML5的優(yōu)勢(shì)越來越大

      3. li inside-block在IE11換行無效的原因

      4. 如何在網(wǎng)站上添加谷歌定位信息

      5. 對(duì)HTML5中表單新增屬性的分析

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)