indexDB數據庫html
參考網址:https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_APIweb
該數據庫是一種存儲在客戶端本篤的NoSQL數據庫,他是事務性數據庫系統數據庫
IndexedDB裏數據以對象的形式存儲,每一個對象都有一個key值索引。IndexedDB裏的操做都是事務性的。一種對象存儲在一個objectStore裏,objectStore就至關於關係數據庫裏的表。IndexedDB能夠有不少objectStore,objectStore裏能夠有不少對象。每一個對象能夠用key值獲取。瀏覽器
這是在各個瀏覽器中都能統一運行spa
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor;
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script> window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor; function versionUpdata(){ var dbName = "indexedDBTest";//數據庫庫名 var dbVersion = 1.1;//版本號 var idb; var dbConnect = indexedDB.open(dbName,dbVersion); //dbConnect對象爲一個IDBOpenDBRequest對象,表明數據庫連接的請求對象 dbConnect.onsuccess = function(e){ idb = e.target.result;//e.target.result對象爲一個IDBDBRequest對象,表明連接成功的數據對象 console.log("e.target.result ::"+e.target.result); } dbConnect.onerror = function(e){ console.log("連接數據庫失敗"); } dbConnect.onupgradeneeded = function(e){//onupgradeneeded連接數據庫是發現指定的版本號大於數據庫當前版本號觸發的事件 idb = e.target.result; var tx = e.target.transaction;//數據版本更新事物 /*@檢測版本號的 var oldVersion = e.oldVersion; var newVersion = e.newVersion; console.log("更新成功,老版本" +oldVersion ,"新版本" + newVersion);*/ /*@建立倉庫 var name = 'Users'; var optionalParameters = { keyPath : 'UserId', autoIncrement : false } var store = idb.createObjectStore(name , optionalParameters); console.log("建立倉庫成功");*/ } } </script> </head> <body> <input type="button" value="連接數據庫" onclick="versionUpdata()"/> </body> </html>
建立indexedDB數據庫中的索引debug
須要經過數據記錄對象的某個屬性值來建立,他只能針對被設爲索引的屬性值進行檢索,不能針對沒有被設爲索引的屬性值進行檢索code
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script> window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; window.IDBCursor = window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor; function CreateIndex(){ var dbName = "indexedDBTest";//數據庫庫名 var dbVersion = 1.1;//版本號 var idb; var dbConnect = indexedDB.open(dbName,dbVersion); //dbConnect對象爲一個IDBOpenDBRequest對象,表明數據庫連接的請求對象 dbConnect.onsuccess = function(e){ idb = e.target.result;//e.target.result對象爲一個IDBDBRequest對象,表明連接成功的數據對象 console.log("e.target.result ::"+e.target.result); } dbConnect.onerror = function(){ console.log("連接數據庫失敗"); } dbConnect.onupgradeneeded = function(e){//onupgradeneeded是咱們惟一能夠修改數據庫結構的地方,在這裏面,咱們能夠建立和刪除對象存儲空間以及構建和刪除索引 debugger; idb = e.target.result; var tx = e.target.transaction;//數據版本更新事物 // @建立倉庫 // 建立一個對象存儲空間來持有有關咱們客戶的信息。 // 咱們將使用 "UserId" 做爲咱們的 key path 由於它保證是惟一的。 var name = 'newUsers'; var optionalParameters = { keyPath : 'UserId', autoIncrement : false } //對象存儲空間僅調用 createObjectStore() 就能夠建立 var store = idb.createObjectStore(name , optionalParameters); console.log("建立倉庫成功"); /* *再建立倉庫成功以後,調用倉庫的createIndex方法建立索引 * @createIndex有3個參數 * 1字符串,表明索引名 * 2參數值表明使用數據倉庫中數據記錄對象的那個屬性來建立索引 * */ var name = 'userNameInter'; // 建立一個索引來經過 name 搜索客戶。 var keyPath = 'useName'; var optionalParameters = { unique:false, //可能會有重複的,所以咱們不能使用 unique 索引。unique的屬性值爲true,表明同一個對象倉庫的兩條數據記錄的索引屬性值(useName)不能相同 multiEntry:false } var idx = store.createIndex(name,keyPath,optionalParameters); console.log("索引建立成功"); } } </script> </head> <body> <input type="button" value="建立索引" onclick="CreateIndex()"/> </body> </html>