IndexedDB学习二:使用
目标
打开数据库连接,开始一个事物创建存储对象请求数据库操作:比如添加或查询数据。等待操作完成, 监听正确的DOM事件。对结果进行其它操作。
创建和构建数据存储使用一个稳定版本的IndexedDB
打开数据库创建和更新数据库版本
onupgradeneeded是修改数据结构的唯一方法。
createObjectStore是创建数据对象的方法。例子中可以通过SSN获取存储对象。添加和删除数据使用游标使用索引设定游标范围和移动方向当WEB应用在另一个TAB中打开时更改版本var openReq = mozIndexedDB.open("MyTestDatabase", 2); openReq.onblocked = function(event) { // If some other tab is loaded with the database, then it needs to be closed // before we can proceed. alert("Please close all other tabs with this site open!");}; openReq.onupgradeneeded = function(event) { // All other databases have been closed. Set everything up. db.createObjectStore(/* ... */); useDatabase(db);} openReq.onsuccess = function(event) { var db = event.target.result; useDatabase(db); return;} function useDatabase(db) { // Make sure to add a handler to be notified if another page requests a version // change. We must close the database. This allows the other page to upgrade the database. // If you don't do this then the upgrade won't happen until the user close the tab. db.onversionchange = function(event) { db.close(); alert("A new version of this page is ready. Please reload!"); }; // Do stuff with the database.
安全
完整的实例
https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB