自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Firefox OS中使用IndexedDB

移動開發(fā)
截至到現(xiàn)在IndexedDB,不同的廠商提供的調(diào)用方式可能會有所差異。本文針對firefox os,對IndexedDB的常用的使用進行說明。本文的示例在firefox os和firfox瀏覽器進行過測試。其他瀏覽器的使用請根據(jù)相關(guān)API文檔進行修改。

截至到現(xiàn)在IndexedDB,不同的廠商提供的調(diào)用方式可能會有所差異。本文針對firefox os,對IndexedDB的常用的使用進行說明。本文的示例在firefox os和firfox瀏覽器進行過測試。其他瀏覽器的使用請根據(jù)相關(guān)API文檔進行修改。

IndexedDB是存儲和快速檢索結(jié)構(gòu)型數(shù)據(jù)的客戶端API,數(shù)據(jù)采用key/value的形式,value可以是結(jié)構(gòu)型的數(shù)據(jù)對象,如json對象。

一、打開一個數(shù)據(jù)庫

  1. //Open IndexedDB
  2. function openDB() {
  3.   var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
  4.   var request = indexedDB.open("B2GDBTest", 1);
  5.   request.onsuccess = function(event) {
  6.     console.log("database open success:" + request.result);
  7.     db = request.result;
  8.   };
  9.   request.onupgradeneeded = function(event) {
  10.    createObjectStore(event.target.result);
  11.   };
  12.   request.onerror = function(event) {
  13.    console.log("database open error:" + request.errorCode);
  14.   };
  15. }

首先需要獲取一個IDBFactory對象,目前不同的瀏覽器獲取的方法有所不同,可以使用下面的寫法來兼容不同的瀏覽器(本文的DEMO只針對firefox os或者firefox瀏覽器)

  1. var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB; 

使用IDBFactory

  1. IDBOpenDBRequest open (in DOMString name, long long version); 

打開數(shù)據(jù)庫的時候需要指定數(shù)據(jù)庫的名字和版本,并按照如下的順序執(zhí)行打開操作

1.如果數(shù)據(jù)庫已經(jīng)存在,會等到versionchange事物結(jié)束后繼續(xù)執(zhí)行,如果打開了一個待刪除的數(shù)據(jù)庫,會等待刪除操作完畢后繼續(xù)執(zhí)行。(注意:這兩個事件并不是本次open數(shù)據(jù)庫產(chǎn)生的,而是其他的操作中產(chǎn)生的未完成的versionchange和刪除操作)

2.如果打開的數(shù)據(jù)庫的版本小于已經(jīng)存在的數(shù)據(jù)庫版本,或終止執(zhí)行,并返回一個類型為VersionError的DOMError

3.如果打開的數(shù)據(jù)庫版本大于已經(jīng)存在的數(shù)據(jù)庫版本,會執(zhí)行versionchange事物,并執(zhí)行onupgradeneeded回調(diào)函數(shù)

4.如果打開的數(shù)據(jù)庫不存在,會創(chuàng)建一個版本為1沒有任何ObjectStore新的數(shù)據(jù)庫

5.連接打開的數(shù)據(jù)庫

二、創(chuàng)建一個ObjectStore

ObjectStore相當與關(guān)系型數(shù)據(jù)庫里的表,你只能在versionchange事物中創(chuàng)建或者刪除ObjectStore,而現(xiàn)在,你只能在上面提到的onupgradeneeded回調(diào)中創(chuàng)建或刪除ObjectStore。創(chuàng)建ObjectStore的操作可以參考如下代碼

  1. //Create Object Store  
  2. function createObjectStore(db) {  
  3.   if (!db) {  
  4.     alert("Database is not open!");  
  5.     return;  
  6.   }  
  7.   if (db.version == 1) {  
  8.  
  9.     if (db.objectStoreNames.contains('customers')) {  
  10.       db.deleteObjectStore("customers")  
  11.     }  
  12.     // Create Object Store  
  13.     // This method was not called from a VERSION_CHANGE transaction callback.  
  14.     var objectStore = db.createObjectStore("customers", {  
  15.       // primary key  
  16.       keyPath : "ssn",  
  17.       // auto increment  
  18.       autoIncrement : true 
  19.     });  
  20.  
  21.     objectStore.createIndex("name", "name", {  
  22.       unique : false 
  23.     });  
  24.  
  25.     objectStore.createIndex("email", "email", {  
  26.       unique : false 
  27.     });  
  28.     console.log("create object store success!");  
  29.   }  

三、數(shù)據(jù)庫的CRUD操作

IndexedDB操作CRUD還是比較容易的,下面分別給出樣例代碼

1.新增

  1. function saveObject() {  
  2.   if (!db) {  
  3.     alert("Database is not open!");  
  4.     return;  
  5.   }  
  6.   var name = document.getElementById("name").value;  
  7.   var email = document.getElementById("email").value;  
  8.   if ("" == name) {  
  9.     alert("name is null!");  
  10.     return;  
  11.   }  
  12.   //Open a transaction with a scope of data stores and a read-write mode.  
  13.   var trans = db.transaction(['customers'], IDBTransaction.READ_WRITE);  
  14.  
  15.   var store = trans.objectStore('customers');  
  16.   var customer = {};  
  17.   customer["name"] = name;  
  18.   customer["email"] = email;  
  19.   var req = store.add(customer);  
  20.   req.onsuccess = function(event) {  
  21.     console.log("save object success!(name:" + name + ",email:" + email + ")");  
  22.   };  
  23.   req.onerror = function(enent) {  
  24.     console.log("save object error:" + req.errorCode);  
  25.   };  

2.修改

  1. //Update Object  
  2. function updateObject() {  
  3.   if (!db) {  
  4.     alert("Database is not open!");  
  5.     return;  
  6.   }  
  7.   var trans = db.transaction(['customers'], IDBTransaction.READ_WRITE);  
  8.  
  9.   var store = trans.objectStore('customers');  
  10.   var customer = {};  
  11.   customer["ssn"] = parseInt(document.getElementById("key").value);  
  12.   customer["name"] = document.getElementById("u_name").value;  
  13.   customer["email"] = document.getElementById("u_email").value;  
  14.   result = store.put(customer);  
  15.   result.onerror = function(event) {  
  16.     console.log("update object error:"+result.errorCode);  
  17.   };  
  18.   result.onsuccess = function(event) {  
  19.     console.log("update object success:ssn:"+customer["ssn"]+",name:"+customer["name"]+",email:"+customer["email"] );  
  20.   };  

3.刪除

  1. //Delete Object()  
  2. function deleteObject(index,obj) {  
  3.   var trans = db.transaction(['customers'], IDBTransaction.READ_WRITE);  
  4.   var store = trans.objectStore('customers');  
  5.   result = store.delete(index);  
  6.   result.onerror = function(event) {  
  7.     console.log("delete Objcet error:"+result.errorCode);  
  8.   }  
  9.   result.onsuccess = function(event) {  
  10.     console.log("delete Object success!");  
  11.     obj.disabled = true;  
  12.   }  
  13. }  

4.查找

  1. //List Objects  
  2. function listObjects() {  
  3.   if (!db) {  
  4.     alert("Database is not open!");  
  5.     return;  
  6.   }  
  7.   var trans = db.transaction(['customers'], IDBTransaction.READ_ONLY);  
  8.   var store = trans.objectStore('customers');  
  9.   var curreq = store.openCursor(IDBKeyRange.bound(1, 4), IDBCursor.PREV);  
  10.   // The "onsuccess" event fires when the cursor is created and  
  11.   // every time the cursor iterates over data.  
  12.   // The following block of code runs multiple times,  
  13.   // until the cursor runs out of data to iterate over.  
  14.   // At that point, the result's request becomes null.  
  15.   var view = document.getElementById("objcetsView");  
  16.   view.innerHTML = "";  
  17.   curreq.onsuccess = function(e) {  
  18.     var cursor = curreq.result;  
  19.     // If the cursor is pointing at something, ask for the data.  
  20.     if (cursor) {  
  21.       var getreq = store.get(cursor.key);  
  22.       // After the data has been retrieved, show it.  
  23.       getreq.onsuccess = function(e) {  
  24.         console.log('key:', cursor.key, 'value:', getreq.result);  
  25.         var value = getreq.result;  
  26.         var objLi=document.createElement("li");  
  27.         objLi.innerHTML = "key:"+cursor.key+",name:"+ value["name"]+",email:"+value["email"]+"<input type='button' value='delete' onclick='deleteObject("+cursor.key+",this)'/>";  
  28.         var updateString = "<input type='button' value='update' onclick='getUpdateObject("+cursor.key + ",\""+value["name"]+"\",\""+value["email"]+"\");'/>";  
  29.         objLi.innerHTML += updateString;  
  30.         view.appendChild(objLi);  
  31.  
  32.         // OK, now move the cursor to the next item.  
  33.         cursor.continue();  
  34.       };  
  35.     }  
  36.   };  

源碼下載:http://chyblog-chyblog.stor.sinaapp.com/wp-content/uploads/2012/09/dbtest.zip

責任編輯:Yeva 來源: chyblog.com
相關(guān)推薦

2012-02-23 15:36:51

IndexedDB

2013-01-14 12:19:48

Firefox OSFirefox OS

2013-01-14 13:21:09

Firefox os

2013-06-24 09:23:25

Firefox OS火狐手機Android

2019-12-10 09:46:12

Elementary PantheonLinux

2013-01-18 10:59:44

IBMdW

2012-09-04 13:35:24

Firefox OS

2012-09-29 10:24:14

Firefox OS

2014-01-09 14:07:46

Firefox OS操作系統(tǒng)

2013-09-02 11:18:06

Firefox OSMarketplace

2012-09-12 09:08:54

Firefox OS

2013-01-14 13:14:11

Firefox OS

2013-01-14 12:40:56

Firefox OS

2014-06-16 10:20:46

Firefox OSWeb Apps

2013-01-14 12:25:49

Firefox OS

2015-11-12 13:47:53

Firefox OSAPPFirefox

2013-01-08 14:58:48

Firefox OS

2009-03-23 09:57:19

2013-02-25 09:15:30

MWC 2013Firefox OS

2013-02-21 13:18:32

點贊
收藏

51CTO技術(shù)棧公眾號