No BB directly on the code
/*Package Indexddb*/varLocalDatabase ={};localdatabase.dbname= "Yilidb"; LOCALDATABASE.INDEXEDDB= WINDOW.INDEXEDDB | | WINDOW.MOZINDEXEDDB | | WINDOW.WEBKITINDEXEDDB | |window.msindexeddb;localdatabase.db=NULL; Localdatabase.idbkeyrange= window. Idbkeyrange | |Window.webkitidbkeyrange;localdatabase.idbcursor= window. Idbcursor | |window.webkitidbcursor;localdatabase.idbtransaction= window. IDbTransaction | |Window.webkitidbtransaction;localdatabase.indexeddb.onerror=function(e) {Console.log ("Database Error:" +e.target.errorcode);};//Deleting a databaseLOCALDATABASE.DELETEINDEXDDB =function() { varDeletedbrequest =localDatabase.indexedDB.deleteDatabase (localdatabase.dbname); Deletedbrequest.onsuccess=function(event) {Console.log (' Database deleted '); Localdatabase.db=NULL; }; Deletedbrequest.onerror=function(e) {Console.log ("Database Error:" +E.target.errorcode); };};//Open DatabaseLOCALDATABASE.OPENINDEXDDB =function(callback) {Console.log ("Init Indexddb"); Try { varOpenRequest =LocalDatabase.indexedDB.open (localdatabase.dbname); Openrequest.onerror=function(e) {callback ("Database Error:" +E.target.errorcode); }; Openrequest.onsuccess=function(event) {localdatabase.db=Openrequest.result; Console.log ("Open indexddb success."); Callback ("Success"); LocalDatabase.db.onversionchange=function() {Console.log ("DB Close"); LocalDatabase.db.close (); }; }; openrequest.onupgradeneeded=function(evt) {Console.log (' Creating Object stores '); varMessagestore = Evt.currentTarget.result.createObjectStore ("message"); Messagestore.createindex ("UserID", "userid", {unique:false }); }; } Catch(E1) {Console.log (E1); }};
Here's another example of the test.
//Add MessageLocaldatabase.addmessagetoindexdb =function(obj, userid) {Try{console.log (localdatabase.db); if(LocalDatabase! =NULL&& localdatabase.db! =NULL) { varTransaction = localDatabase.db.transaction ("message", "ReadWrite"); varstore = Transaction.objectstore ("message"); varRequest =Store.get (userid); Request.onsuccess=function(e) {varresult =E.target.result; varexist = 0; if(typeofResult! = ' undefined ') { for(vari = 0; i < result.length; i++) { if(Result[i].userid = =Obj.userid) {Result[i]=obj; exist++; Break; } } if(!exist) {Result.push (obj); } store.put (result, UserID); } Else{store.add ([obj], userid); } }; } } Catch(e) {Console.log (e)}};//Get MessageLocaldatabase.getmessage =function(UserID, callback) {Try { if(LocalDatabase! =NULL&& localdatabase.db! =NULL) { varstore = localDatabase.db.transaction ("message"). ObjectStore ("message")); //var index = store.index ("userid"); //var request = Index.opencursor (Idbkeyrange.only (userid));Request =Store.get (userid); Request.onsuccess=function(res) {varresult =Res.target.result; if(typeofResult! = ' undefined ') {callback (result); } Else{Callback (NULL); } }; } } Catch(e) {console.log (e); }};
HTM5 Local Storage Scenario--INDEXDB encapsulation