indexedDB資料庫的基本概念:在HTML5中,新增一種被稱為“indexedDB”的資料庫,該資料庫是一種存儲在客戶端本地的NoSQL資料庫。 顯示效果: ...
indexedDB資料庫的基本概念:在HTML5中,新增一種被稱為“indexedDB”的資料庫,該資料庫是一種存儲在客戶端本地的NoSQL資料庫。
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <script> 7 window.indexedDB=window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB; 8 window.IDBTransaction= window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; 9 window.IDBKeyrange=window.IDBKeyrange || window.webkitIDBKeyrange ||window.msIDBKeyrange; 10 window.IDBCursor= window.IDBCursor || window.webkitIDBCursor || window.msIDBCursor; 11 12 function CreateObjectStore() { 13 var dbName = "indexedDBtest"; 14 var dbVersion = 3; 15 var idb; 16 var dbConnect = indexedDB.open(dbName,dbVersion); 17 dbConnect.onsuccess = function (e) { 18 idb = e.target.result; 19 alert("資料庫鏈接成功"); 20 }; 21 dbConnect.onerorr = function () { 22 alert("鏈接資料庫失敗"); 23 } 24 dbConnect.onupgradeneeded = function (e) { 25 idb = e.target.result; 26 var name = "user"; 27 var optionalParameters = { 28 keyPath:"userid", 29 autoIncrement:false 30 }; 31 var store = idb.createObjectStore(name,optionalParameters); 32 alert("對象倉庫創建成功"); 33 } 34 } 35 </script> 36 </head> 37 <body> 38 <input type="button" value="創建倉庫" onclick="CreateObjectStore()"> 39 </body> 40 </html>
顯示效果: