Skip to content

Instantly share code, notes, and snippets.

@xuyanbo03
Created January 23, 2018 11:37
Show Gist options
  • Save xuyanbo03/65be6d79baac672c2a8fd5a6dfa4c1c9 to your computer and use it in GitHub Desktop.
Save xuyanbo03/65be6d79baac672c2a8fd5a6dfa4c1c9 to your computer and use it in GitHub Desktop.
IndexedDB learn
function openDB(name,callback){
var request=window.indexedDB.open(name);
request.onsuccess=function(e){
myDB.db=e.target.result;
callback();
}
request.onupgradeneeded=function(e){
var store =e.target.result.createObjectStore('books',{
keyPath:'isbn'
});
var titleIndex=store.createIndex('by_title','title',{
unique:true
});
var authorIndex=store.createIndex('by_author','author');
store.put({
title:'Q',
author:'Ab',
isbn:123456
});
store.put({
title:'A',
author:'Ab',
isbn:234567
});
store.put({
title:'B',
author:'Ab',
isbn:345678
});
}
}
var myDB={
name:'test',
version:'1.0',
db:null
}
function addDate(db){
var transaction=db.transaction('books','readwrite');
var store=transaction.objectStore('books');
// var request=store.get(234567);
// request.onsuccess=function(e){
// console.log(e.target.result);
// }
store.add({
title:'B',
author:'Ab',
isbn:222
});
//store.delete(222);
store.get(222).onsuccess=function(e){
books=e.target.result;
books.author='aa';
var request=store.put(books);
request.onsuccess=function(e){
console.log("update success");
}
}
}
openDB(myDB.name,function(){
//myDB.db.close();
});
setTimeout(function(){
addDate(myDB.db)
},2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment