码迷,mamicode.com
首页 > 其他好文 > 详细

表的增删查改

时间:2018-06-21 00:03:04      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:let   ons   created   web   log   webkit   ted   upgrade   path   

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‘)
}

}

表的增删查改

标签:let   ons   created   web   log   webkit   ted   upgrade   path   

原文地址:https://www.cnblogs.com/xm16/p/9206473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!