zoukankan      html  css  js  c++  java
  • indexedDB

     indexedDB兼容性

     代码:

    class indexedDB {
        constructor(){
            this.indexedDBInstanceObject = null;
        }
        openIndexedDB(dbName = 'cacheList', version = 1){
            let request = window.indexedDB && window.indexedDB.open(dbName, version);
            request.onerror = (error) => {
                console.error('数据库打开报错:',error);
            }
            request.onsuccess = (result) => {
                this.indexedDBInstanceObject = request.result;
                console.info('数据库打开成功:',result);
            }
            request.onupgradeneeded = (event) => {
                this.indexedDBInstanceObject = event.target.result;
            }
        }
        createTable(tableName = 'person', params = { keyPath: 'id'}){
            let objectStore = null;
            if(!this.indexedDBInstanceObject.objectStoreNames.contains(tableName)){
                objectStore = this.indexedDBInstanceObject.createObjectStore(tableName, params);
            }
            return objectStore;
        }
        createIndex(indexArr = []){
            let objectStore = this.createTable();
            indexArr.forEach(item => {
                let { indexName, indexKey, indexObj } = item;
                objectStore.createIndex(indexName, indexKey, indexObj);
            });
        }
        add(tableName, data){
            let request = this.indexedDBInstanceObject.transaction([tableName], 'readwrite').objectStore(tableName).add(data);
            request.onsuccess = () => {
                console.log('数据写入成功');
            }
            request.onerror = () => {
                console.log('数据写入失败');
            }
        }
        read(tableName, mainKey = 1){
            let request = this.indexedDBInstanceObject.transaction([tableName]).objectStore(tableName).get(mainKey);
            request.onsuccess = () => {
                if(request.result){
                    console.log(request.result);
                }else{
                    console.log('未获得数据');
                }
            }
            request.onerror = () => {
                console.log('读取失败');
            }
        }
        put(tableName, data){
            let request = this.indexedDBInstanceObject.transaction([tableName], 'readwrite').objectStore(tableName).put(data);
            request.onsuccess = () => {
                console.log('数据更新成功');
            }
            request.onerror = () => {
                console.log('数据更新失败');
            }
        }
        remove(tableName, mainKey = 1){
            let request = this.indexedDBInstanceObject.transaction([tableName], 'readwrite').objectStore(tableName).delete(mainKey);
            request.onsuccess = () => {
                console.log('数据删除成功');
            }
            request.onerror = () => {
                console.log('数据删除失败');
            }
        }
        readByIndex(index, value){
            let request = this.indexedDBInstanceObject.transaction([tableName], 'readonly').objectStore(tableName).index(index).get(value);
            request.onsuccess = () => {
                if(request.result){
                    console.log(request.result);
                }else{
                    console.log('未获得数据');
                }
            }
            request.onerror = () => {
                console.log('读取失败');
            }
        }
    }
    以自己现在的努力程度,还没有资格和别人拼天赋
  • 相关阅读:
    nginx优化配置
    mysql查看变量/配置文件位置
    关于ubuntu的ssh远程登录的问题
    ubuntu镜像下载地址
    百度地图标注地点
    Yii常用方法
    python_将一组数据展示成直方图(以list为例)
    opencv_形态学结构化元素对形态学图像处理的影响
    C语言学习_从VC++6.0开始
    SVM原理(1)
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/14629940.html
Copyright © 2011-2022 走看看