zoukankan      html  css  js  c++  java
  • PhoneGap HTML5 DB操作类, 做本地存储

    类库优点:
    1. 快速建表
    2. 封装CURD操作,更加简洁
    3. 兼容PhoneGap

    4. 自己用着顺手,简化操作

    void function(version){ 
         
        var app = function(e) {
            _this.DB;
        }
     
        /**
         * 初始化数据库 
         * @param {String} dbName
         * @param {String} dbVersion
         * @param {String} dbDesc
         * @param {String} dbSize
         */
        app.prototype.dbConnect = function(dbName,dbVersion,dbDesc,dbSize){
             
            try {
                if (!window.openDatabase) {
                    console.log('Databases are not supported in this browser.');
                    return false;
                } else {
                    dbName      = dbName ? dbName : 'SHICAI_APP';
                    dbVersion   = dbVersion ? dbVersion : '1.0';
                    dbDesc      = dbDesc ? dbDesc : 'SHICAI_DB for User Mobile';
                    dbSize      = dbSize ? dbSize : (2 * 1024 * 1024);
                     
                    _this.DB = openDatabase(dbName, dbVersion, dbDesc, dbSize); 
                     
                    return true;
                }
            } catch(e) {
                if (e == 2) {
                    console.log("Invalid database version.");
                } else {
                    console.log("Unknown error "+e+".");
                }
                return false;
            }
             
        }
         
        /**
         * 创建表
         * @param {String} tableName
         * @param {Object} tableField
         */
        app.prototype.dbDefineTable = function(tableName,tableField){
         
            if(!tableName || !tableField){
                console.log('ERROR: Function "dbCreateTable" tableName or tableField is NULL.');
            }
            var fieldArr = [];
            var fieldItem;
            var i = 0;
             
            for (var field in tableField){
                field.toString();
                tableField[field].toString();
                fieldArr[i] = field+' '+tableField[field];
                 
                i++;
            }
            fieldItem = fieldArr.join(",").toString();
             
            var SQL = 'CREATE TABLE IF NOT EXISTS '+tableName+' (';
            SQL += fieldItem;
            SQL +=')';
            console.log(SQL);
             
            _this.DB.transaction(function(tx){
                tx.executeSql(SQL,[],function(tx,result){
                    return true;
                },function(tx,error){
                    console.log(error);
                    return false;
                });
            }); 
        }
         
        /**
         * 插入数据
         * @param {String} tableName
         * @param {Object} tableField
         * @param {Function} funName
         */
        app.prototype.dbInsert = function(tableName,tableField,funName){
         
            if(!tableField){
                console.log('ERROR: FUNCTION dbInsert tableField is NULL');
                return false;
            }
             
            var fieldKeyArr = [];
            var fieldValueArr = [];
            var fieldKey;
            var fieldValue;
            var i = 0;
             
            for (var field in tableField){
             
                field.toString();
                tableField[field].toString();
                fieldKeyArr[i] = field;
                fieldValueArr[i] = tableField[field];
                if(typeof(fieldValueArr[i]) !== 'number'){
                    fieldValueArr[i] = '"'+fieldValueArr[i]+'"';
                }
                i++;
            }
            fieldKey = fieldKeyArr.join(",");
            fieldValue = fieldValueArr.join(",");
     
            var SQL = 'INSERT INTO '+tableName+' (';
            SQL += fieldKey;
            SQL += ') ';
            SQL += 'VALUES (';
            SQL += fieldValue;
            SQL += ')';
            console.log(SQL); 
             
            _this.DB.transaction(function(tx){
                tx.executeSql(SQL,[],function(tx,result){
                    funName(result);
                },function(tx,error){
                    console.log(error);
                    return false;
                });
            }); 
        }
         
        /**
         * 查询所有结果
         * @param {String}  tableName
         * @param {Function} funName
         * @param {Object}  tableField
         * @param {Object}  dbParams
         */
        app.prototype.dbFindAll = function(tableName,funName,tableField,dbParams){
     
            tableField = tableField ? tableField : '*';
            if(!tableName || !funName){
                console.log('ERROR: Function "dbFindAll" tableName or funName is NULL.');
            }
             
            var SQL = '';
            SQL +='SELECT '+tableField+' FROM '+tableName;
             
            _this.DB.transaction(function(tx){
                tx.executeSql(SQL,[],_findSuccess,function(tx,error){
                    console.log(error);
                    return false;
                });
            });
             
            function _findSuccess(tx,result){
                funName(result);
            }
     
        }
         
        /**
         * 删除数据
         * @param {String}  tableName
         * @param {Object}  dbParams
         * @param {Function} funName
         */
        app.prototype.dbDelete = function(tableName,dbParams,funName){
         
            if(!tableName || !dbParams){
                console.log('ERROR: FUNCTION "dbDelete" tableName or dbParams is NULL');
                return false;
            }
            var SQL = '';
            SQL +='DELETE FROM '+tableName+' WHERE ';
             
            var paramArr = new Array();
            var paramStr = '';
            var i=0;
            for(var k in dbParams){
                if(typeof(dbParams[k]) !== 'number'){
                    dbParams[k] = '"'+dbParams[k]+'"';
                }
                paramArr[i] = k.toString()+'='+dbParams[k];
                i++;
            }
            paramStr = paramArr.join(" AND ");
            SQL += paramStr;
             
            _this.DB.transaction(function(tx){
                    tx.executeSql(SQL);
                },[],function(tx,result){
                    funName(result);
                },function(tx,error){
                    console.log(error);
                    return false;
                });
            console.log(SQL);
        }
         
        /**
         * 更新数据表
         * @param {String}  *tableName
         * @param {Object}  *dbParams
         * @param {Object}  *dbWhere
         * @param {Function} funName
         */
        app.prototype.dbUpdate = function(tableName,dbParams,dbWhere,funName){
     
            var SQL = 'UPDATE '+tableName+' SET ';
            var paramArr = new Array();
            var paramStr = '';
            var i=0;
            for(var k in dbParams){
                if(typeof(dbParams[k]) !== 'number'){
                    dbParams[k] = '"'+dbParams[k]+'"';
                }
                paramArr[i] = k.toString()+'='+dbParams[k];
                i++;
            }
            paramStr = paramArr.join(" , ");
             
            SQL += paramStr;
            SQL += ' WHERE ';
             
            var whereArr = new Array();
            var whereStr = '';
            var n=0;
            for(var w in dbWhere){
                 
                if(typeof(dbWhere[w]) !=='number'){
                    dbWhere[n] = '"'+dbWhere[w]+'"';
                }
                whereArr[n] = w.toString()+'='+dbWhere[w];
                n++;
            }
             
            whereStr = whereArr.join(" AND ");
             
            SQL += whereStr;
             
            _this.DB.transaction(function(tx){
                    tx.executeSql(SQL);
                },[],function(tx,result){
                    funName(result);
                },function(tx,error){
                    console.log(error);
                    return false;
                });
            console.log(SQL);
             
        }
         
        /**
         * 清空数据表
         * @param {String} tableName
         * @return {Boolean}
         */
        app.prototype.dbTruncate = function(tableName){
         
            if(!tableName){
                console.log('ERROR:Table Name is NULL');
                return false;
            }
             
            function _TRUNCATE(tableName){
                _this.DB.transaction(function(tx){
                    tx.executeSql('DELETE TABLE '+tableName);
                },[],function(tx,result){
                    console.log('DELETE TABLE '+tableName);
                    return true;
                },function(tx,error){
                    console.log(error);
                    return false;
                })
            }
             
            _TRUNCATE(tableName);
        }
         
        /**
         * @desc 删除数据表
         * @param {String} tableName
         * @return {Boolean}
         */
        app.prototype.dbDrop = function(tableName){
             
            if(!tableName){
                console.log('ERROR:Table Name is NULL');
                return false;
            }
             
            function _DROP(tableName){
                _this.DB.transaction(function(tx){
                    tx.executeSql('DROP TABLE '+tableName);
                },[],function(tx,result){
                    console.log('DROP TABLE '+tableName);
                    return true;
                },function(tx,error){
                    console.log(error);
                    return false;
                })
            }
             
            _DROP(tableName);
        }
         
    }('1.0');



  • 相关阅读:
    PHP Laravel Install and Quickstart
    PHP Composer
    PHP学习 Cookie和Session
    PHP学习 Object Oriented 面向对象 OO
    PHP学习 例外和错误处理
    PHP学习 文件访问和写入
    PHP学习 函数 function
    PHP学习 流程控制和数组
    修改docker镜像地址为阿里云
    通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态
  • 原文地址:https://www.cnblogs.com/fyq891014/p/3294758.html
Copyright © 2011-2022 走看看