var db=window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var request, result, version=2,
dbName = ‘textDB‘,
osName = ‘its‘;
function createDB(){
request=db.open(dbName,version);
request.onsuccess=function(){
db = request.result;
}
request.onerror=function(e){
console.log(e.currentTarget.error.message)
}
request.onupgradeneeded=function(e){
db = request.result;
if (!db.objectStoreNames.contains(osName)) {
db.createObjectStore(osName,{keyPath: ‘id‘})
}
}
}
createDB()
//save date
var date=[{
name: ‘史莱姆‘,
id: ‘001‘,
hp: 3
},{
name: ‘小蝙蝠‘,
id: ‘002‘,
hp: 5
},{
name: ‘小恶魔‘,
id: ‘003‘,
hp: 9
}];
function addDate(){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
date.map(function(o){
store.add(o);
})
}
function getDate(id){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
var request = store.get(id);
request.onsuccess=function(){
console.log(request.result)
}
}
function getAllDate(){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
var request = store.getAll();
request.onsuccess=function(){
console.log(request.result)
}
}
function updateDate(id){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
var request = store.get(id);
request.onsuccess=function(){
request = store.put({
name: ‘小狗狗‘,
id: ‘id‘,
hp: 9
})
}
}
function deleteDate(id){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
var request = store.delete(id);
request.onsuccess = function(){
console.log(‘delete success‘)
}
}
function clear(){
var transaction = db.transaction(osName,‘readwrite‘),
store = transaction.objectStore(osName);
var request = store.clear();
request.onsuccess = function(){
console.log(‘clear success‘)
}
}
原文地址:https://www.cnblogs.com/xm16/p/9206473.html