zoukankan      html  css  js  c++  java
  • 表的增删查改

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

    }

  • 相关阅读:
    Some Depth Theory
    Hom和$otimes$如何把我绕晕
    A natrual way to introduce homotopy groups
    指数多项式的Galois群计算
    Trianglated Category and Derived Categories
    最近的代数课上的一些灵魂提问
    致青年朋友的一封信 莫洛亚
    一个函数证明题
    游客的故事
    14. 运算符重载(二)
  • 原文地址:https://www.cnblogs.com/xm16/p/9206473.html
Copyright © 2011-2022 走看看