jquery的indexeddb使用
项目地址: github.com/axemclion/jquery-indexeddb/blob/gh-pages/docs/README.md
工具类var MyDb = { DB_NAME : 'PaperData', DB_VERSION : 3 , //使用正整数,别用浮点型 initDb :function() { console.debug("initDb ..."); var req = indexedDB.open(MyDb.DB_NAME,MyDb.DB_VERSION); req.onsuccess = function (evt) { db = evt.target.result; console.debug("initDb opened"); }; req.onerror = function (evt) { console.error("initDb error:", evt.target.errorCode || evt.target.error); }; req.onupgradeneeded = function (evt) { db = evt.target.result; $(db.objectStoreNames).each(function (index,data) { db.deleteObjectStore(data) }); var store= db.createObjectStore("questions",{ "keyPath":"info" }); store.createIndex('paperIndex','paper',{unique:false}); //store.createIndex('nameIndex','name',{unique:false}); }; }, getDB: function () { return $.indexedDB(MyDb.DB_NAME,MyDb.DB_VERSION) }};
//db的初始化$(function () { MyDb.initDb(); var db = MyDb.getDB(); //此时可以使用的db,这个db可以保存为全局变量 });
//保存到questions里面的信息,put表示没有就加入,有就更新db.objectStore("questions").put({info:"paper_"+paper_info.paper_id+"_"+name,paper:paper_info.paper_id,name:name,value:value})
//普通查询,获取满足要求的key=1或key=2的记录的所有记录db.objectStore("questions").each(function (data) { console.log(data)},[1,2])
//根据paperIndex获取paper为100的值,也可以是范围[100,110](这个是根据索引获取,效率比较高)db.objectStore("questions").index("paperIndex").each(function(data){ console.log(data)}, 100)