HTML5 sqllite數據庫操作工具函數

/**
 * DBCommon 提供基本的數據庫創建,表創建,表增刪改查,批量增刪改查操作
 * 1,數據庫創建(同步方式,注意判斷瀏覽器是否支持,數據庫是否創建成功)
 * 2,表的創建,公共方法,輸入參數包括表名稱,字段名稱 注意回調函數
 * 3,數據增加,公共方法,輸入參數包括表名稱,各個字段名 
 * 4,數據修改,公共方法,輸入參數包括表名稱,修改字段值,條件
 * 5,數據刪除,公共方法,輸入參數包括表名稱,條件 
 * 6,表刪除,公共方法,輸入參數包括表名稱
 * 7,表查詢,公共方法,輸入參數包括表名稱,查詢條件,結果轉換
 * 
 */
/**
 * DBCommon 構造函數,創建數據庫
 * 
 * @param dbName 數據庫名稱
 * @param version 數據庫版本號
 * @param displayName 數據庫描述
 * @param maxSize 數據庫最大容量,沒有不填
 * @param debug 是否開題日誌記錄
 */
function DBCommon(dbName, version, displayName, maxSize,debug) {
         this.debug = debug;
         try {  
            if (!window.openDatabase) {  
                alert('This browser does not support local database!');  
            } else {  
            if(maxSize){
               this.db = openDatabase(dbName, version, displayName, maxSize);
            }else{
               this.db = openDatabase(dbName, version, displayName);
            }
            }  
        } catch(e) {
            alert('Create local database fail!'); 
        }
}

DBCommon.prototype = {
 log : function( msg ){
        if( this.debug) {
                console.log.apply(console, arguments);
            }
        },

sqlerrorHandler :  function(tx, e) {
       console.log.error(e.message);
    },

getConnection : function (dbName, version, displayName, maxSize) {
	 var dbase;
	 try {  
	    if (!window.openDatabase) {  
	        alert('This browser does not support local database!');  
	    } else {  
		    if(maxSize){
		       dbase = openDatabase(dbName, version, displayName, maxSize);
		    }else{
		       dbase = openDatabase(dbName, version, displayName);
		    }
	    }
	} catch(e) {
	    alert('Create local database fail!'); 
	}
	return dbase;
},
/**
 * 創建表
 * 
 * @param tableName 表名稱
 * @param fields 字段數組
 */
createTable : function (tableName, fields, callBack) {
       
       var sql = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + fields.join(",") + ")";
        this.db.transaction(function (tx) {
             tx.executeSql(sql, [], function () {
                if (callBack) callBack();
            }, this.sqlerrorHandler);
        })
        log(sql);
        return this;
    },
/**
 * 刪除表
 * 
 * @param tableName 表名稱
 */
dropTable : function (tableName) {
        var sql = "DROP TABLE " + tableName;
         this.db.transaction(function (tx) {
            tx.executeSql(sql);
         })
          log(sql);
         return this;
 },
 /**
  * 添加數據
  * 
  * @param tableName 表名稱
  * @param fields 字段名稱數組
  * @param values 字段值數組
  * @param callback 回調函數
  */
    insert :  function (tableName, fields, values, callback) {
     var sql = "INSERT INTO " + tableName + " (" + fields.join(",") + ") VALUES("
         + new Array(fields.length + 1).join(",?").substr(1) + ")";
         this.db.transaction(function (tx) {
             tx.executeSql(sql, values, function (tx , rs) {if (callback) callback(tx , rs);} , this.sqlerrorHandler);
        });
        log(sql+values);
         return this;
     },
/**
 * 刪除記錄
 * @param tableName 表名稱
 * @param pkField 字段名稱
 * @param value 字段值
 * @param callback 
 * @return
 */     
deleteRow : function (tableName, pkField, value, callback){
      var sql = "DELETE FROM " + tableName + " WHERE " + pkField + " = ?";
         this.db.transaction(function (tx) {
             tx.executeSql(sql, [value], null, this.sqlerrorHandler);
              if (callback) callback();  
         })
         log(sql+value);
         return this;
     },
     
/**
 * 刪除所有記錄
 * @param tableName 表名稱
 * @param pkField 字段名稱
 * @param value 字段值
 * @param callback 
 * @return
 */     
delete : function (tableName, callback){
      var sql = "DELETE FROM " + tableName;
         this.db.transaction(function (tx) {
             tx.executeSql(sql, null, null, this.sqlerrorHandler);
              if (callback) callback();  
         })
         log(sql);
         return this;
     },

/**
 * 更新記錄(字段名稱數組中第一個字段填主鍵名稱,所對應的values的第一個值填主鍵的值)
 * @param tableName 表名
 * @param fields 字段名稱數組
 * @param values 字段值數組
 * @param callback
 * @return
 */
update :  function (tableName, fields, values,callback) {
        var len = fields.length;

        var sql = "";
        for (i = 1; i < len; i++) {
           if (i == 1) sql += fields[i] + " = '" + values[i] + "'";
            else sql += "," + fields[i] + " = '" + values[i] + "'";
        }
         sql = 'UPDATE ' + tableName + ' SET ' + sql + ' where ' + fields[0] + '= ?';
 
        this.db.transaction(function (tx) {
             tx.executeSql(sql, [values[0]],
             null, this.sqlerrorHandler);
             if (callback) callback();
         });
         log(sql+values[0]);
        return this ;
     },
/**
 * 查詢單行數據,在回調函數中處理返回數據
 * @param tableName 表名
 * @param pkField 字段名
 * @param value 字段值
 * @param callback
 * @return
 */
queryById :  function (tableName, pkField, value, callback) {
     var sql = 'SELECT * FROM ' + tableName + ' WHERE '+pkField + '= ?'
       this.db.transaction(function (tx) {
            tx.executeSql(sql, [value], function (tx, result) {
                 if (callback) 
                 {
                     var rows = result.rows;
                     if (rows.length != 0) {
                       callback(result.rows.item(0)); 
                     }else{
                        callback(null);
                     }
                 }
             }, this.sqlerrorHandler);
        });
        log(sql+value);
        return this ;
    },

/**
 * 查詢整張表數據
 * @param tableName 表名
 * @param callback
 * @return
 */
queryAll :  function (tableName, callback) {
     var sql = 'SELECT * from ' + tableName;
         this.db.transaction(function (tx) {
             tx.executeSql(sql, [], function (tx, result) {
                 if (callback) callback(result);                
             }, this.sqlerrorHandler);
        });
       log(sql);
        return this ;
    },

/**
 * 根據條件查詢數據
 * @param tableName 表名
 * @param queryConditions 查詢條件
 * @param callback
 * @return
 */
query :  function (tableName, queryConditions, callback) {
     var sql = 'SELECT * from ' + tableName+" WHERE "+ queryConditions
        this.db.transaction(function (tx) {
            tx.executeSql(sql, [], function (tx, result) {
               if (callback) callback(result);              
            }, this.sqlerrorHandler);
        }); 
        log(sql);
         return this;
    },


/**
 * 根據條件最大值
 * @param tableName 表名
 * @param queryConditions 查詢條件
 * @param callback
 * @return
 */
queryMaxId :  function (tableName, callback) {
     var sql = 'SELECT MAX(ID) id from ' + tableName;
        this.db.transaction(function (tx) {
            tx.executeSql(sql, [], function (tx, result) {
               if (callback) callback(result);              
            }, this.sqlerrorHandler);
        }); 
        log(sql);
         return this;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章