• 表的增删查改


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

    }

  • 相关阅读:
    linux分析、诊断及调优必备的“杀器”之二
    浏览器被“hao123.3377.com”主页劫持的解决办法
    Nginx一个实现负载均衡的强大web server
    【百度之星
    c/c++ 参数传递
    【百度之星】-IP聚合
    百度之星-大搬家-递推
    IOS开发之Swift学习笔记
    【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle
    【Leetcode】82. Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/xm16/p/9206473.html
Copyright © 2020-2023  润新知