IndexdDB簡介
html5中indexdDB是一種能在瀏覽器持久的存儲結構化數據的數據庫;且具有豐富的查詢能力。
新建一個IndexdDB數據庫
IDBOpenDBRequest定義有幾個重要的屬性:
- onerror:新建或打開數據庫失敗的回調
- onsuccess:新建或打開數據庫成功的回調
- onupgradeneeded:新建或打開數據庫版本發生變化時觸發的回調
以上的幾個API均是異步執行,所以我們在它們的回調函數中處理我們需要的數據。
1 function createIndexDB(dbName,version) {//根據數據庫名稱和數據庫版本號來打開或者新建一個數據庫 2 var version = version || 1; 3 var request = window.indexedDB.open(dbName,version); 4 request.onerror = function(e) { //新建或打開數據失敗的回調函數 5 console.log(e); 6 }; 7 8 request.onsuccess = function(e) { //新建或打開數據庫成功的回調 9 mydb.db = e.target.result; // 把result賦值給對象屬性 10 }; 11 12 //初始化object store 也可以//刪除object store 13 request.onupgradeneeded = function(e){ 14 var db = e.target.result; 15 if(!db.objectStoreNames.contains('Employees')) { //是否包含員工這個對象 16 //db.createObjectStore('Employees',{keyPath:'id'}); // keyPath 指定對象的哪個屬性作為主鍵 17 //db.createObjectStore('Employees',{autoIncrement:true}); //keyGenerate 主鍵自增長 ,任意值, 18 var store = db.createObjectStore('Employees',{keyPath:'id'}); // 同時返回一個store 19 store.createIndex('nameIndex','name',{unique:true}); //創建name的索引 20 store.createIndex('ageIndex','age',{unique:true});//創建age的索引 21 } 22 /*//刪除object store 23 if(db.objectStoreNames.contains('Employees')) { //先查詢是否存在 24 db.deleteObjectStore('Employees'); 25 }*/ 26 27 console.log("db version change to" + version); 28 }; 29 30 }
新建數據庫成功:
初始化一些數據:
1 var mydb = { 2 name:'firstDB', 3 version:1, //只能是整數,3.31--->會轉成3 4 db:null 5 }; 6 // employee數據 7 var Employee= [{ 8 id:'lx001', 9 name:'lucas', 10 age:20 11 }, 12 { 13 id:'lx002', 14 name:'lucy', 15 age:18 16 }, 17 { 18 id:'lx003', 19 name:'mike', 20 age:22 21 }, 22 { 23 id:'lx004', 24 name:'susan', 25 age:21 26 } 27 ];
添加數據Create:
1 //添加數據 2 function addData(db,storename){ 3 var transaction = db.transaction(storename,"readwrite");//首先獲取事務 4 var store = transaction.objectStore(storename);//根據事務獲取對應的storename獲取我們新建數據庫的store 5 for(var i = 0; i < employee.length; i++) {//遍歷我們的數據 6 store.add(employee[i]);//add到store中 7 } 8 }
調用addData函數添加數據:addData(mydb.db, 'Employee')
成功添加數據;
indexdDB中事務
數據庫都會有事務操作,indexdDB中根據數據庫名字即read/write來獲取事務,如添加數據庫第一句代碼。事務中要指定需要的object store;同時事務中具有3中模式:
- read:只讀,不可修改數據庫可以並發執行;
- readwrite:讀寫,可以修改數據庫;
- verionchange:數據庫版本變更
indexdDB中的主鍵,索引,游標
主鍵:在新建數據庫的時候指定數據的某個字段作為主鍵且該字段具有唯一性;如本文中就指定了id作為了主鍵(keyPath)。同時也可以使用自動增長數字作為鍵值(keyGenerator);同時也可以不指定。
索引:索引頁是數據庫中高效查詢的一種方法;同時indexdDB也提供了創建索引的功能;如上新建數據庫的時候我們就順便創建了兩個索引(nameIndex/ageIndex)。創建索引需要3個參數:索引名稱,創建索引的字段,索引屬性是否唯一;
游標:有了索引沒有游標索引豈不是很寂寞,indexdDB同時也提供了創建游標的方法;我們可以使用游標來遍歷object store了;
下面是創建游標:
1 //使用游標來獲取值,結合索引 2 function getDataByFetch(db, storename,indexType, indexName) { 3 var transaction = db.transaction(storename); 4 var store = transaction.objectStore(storename); 5 var index = store.index(indexType); // 6 var request = index.openCursor(IDBKeyRange.only(indexName)); //打開游標,得到一個請求;store打開的是哪個類型的索引,only中就是哪個索引的值 7 request.onsuccess = function(e) { 8 var cursor = e.target.result; 9 if(cursor) { 10 console.log(cursor.key) 11 var e = cursor.value; 12 console.log(e); 13 cursor.continue(); //游標下一位,沒有會返回undefined 14 } 15 } 16 }
游標創建完成;
平常數據庫中常常使用的CRUD,indexdDB也提供了相應的方法;上面已經介紹了添加數據
查詢數據 Retrieve:

1 //根據id獲取數據 2 function getDataByKey(db,storename,id){ 3 var transaction = db.transaction(storename,'readwrite');//先獲取事務 4 var store = transaction.objectStore(storename);//獲取object store 5 var req = store.get(id); // 根據指定的keyPath的值查詢 即id值 6 req.onsuccess = function(e) { 7 var employee = e.target.result; 8 console.log(employee.name); 9 } 10 } 11 //利用索引來獲取值 12 function getDataByIndex(db,storename,indexType, indexName) { 13 var transaction = db.transaction(storename);//根據storename獲取事務 14 var store = transaction.objectStore(storename); //storename為參數用事務獲取store 15 var index = store.index(indexType);//使用index方法獲取IDBIndex對象 16 index.get(indexName).onsuccess = function(e) { 17 var e = e.target.result; 18 console.log(e); 19 } 20 } 21 22 //同時也可以使用游標來獲取值,結合索引 23 function getDataByFetch(db, storename,indexType, indexName) { 24 var transaction = db.transaction(storename); 25 var store = transaction.objectStore(storename); 26 var index = store.index(indexType); // 27 var request = index.openCursor(IDBKeyRange.only(indexName)); //打開游標,得到一個請求;store打開的是哪個類型的索引,only中就是哪個索引的值 28 request.onsuccess = function(e) { 29 var cursor = e.target.result; 30 if(cursor) { 31 console.log(cursor.key) 32 var e = cursor.value; 33 console.log(e); 34 cursor.continue(); //游標下一位,沒有會返回undefined 35 } 36 } 37 }
查詢結果:
更新數據 update:
// 更新數據 function updateData(db, storename, id){ var transaction = db.transaction(storename, 'readwrite'); //獲取事務 var store = transaction.objectStore(storename);// 得到指定的object store var req = store.get(id);//根據id查找指定的對象 req.onsuccess = function(e) { var employee = e.target.result;//得到對象 employee.age = 19;//修改值 store.put(employee);//將修改之后的值放回指定的object store中 }; }
刪除數據 delete:
1 // 根據id刪除指定數據 2 function deleteData(db, storename, id){ 3 var transaction = db.transaction(storename, 'readwrite');//獲取事務 4 var store = transaction.objectStore(storename); // 得到對應的object store 5 store.delete(id);// 根據id刪除對應store中的數據 6 }
清空object store
1 //清空object store 2 function clearStore(db, storename) { 3 var transaction = db.transaction(storename,'readwrite'); // 首先獲取事務 4 var store = transaction.objectStore(storename); //其次得到指定的object store 5 store.clear(); // store調用clear方法 6 }
怎么樣, 是不是覺得indexdDB很強大呢?以上都是自己的一些個人學習收獲,如有不對的地方歡迎大家批評指正。謝謝!