Web SQL Database工具类

 

   注意:如果dbUtil内的方法要对外开放,则需在方法前面加 this.

 

/*管理本地sql相关脚本*/

var dbUtil = function () {

    var dbName = 'wydb'; //数据库名
    var version = '1.0'; //版本信息
    var description = 'wydb'; //描述
    var maxSize = 1024 * 1024 * 1024; //最大值
    var dbObj = null;

    //打开数据库
    function openDB() {
        try {
            if (!dbObj) {
                dbObj = openDatabase(dbName, version, description, maxSize);
            }
        } catch (e) {
            alert("打开数据库出现未知错误: " + e);
            dbObj = null;
        }
        return dbObj;
    }

    getDB = function () {
        openDB();
        return dbObj;
    }

    executeSqlDefault = function (sqlStr, params, successHandler, errorHandler) {
        openDB();
        dbObj.transaction(function (tx) {
            tx.executeSql(sqlStr, params, successHandler, errorHandler);
        }, null, null);
    }

    executeSqlTrans = function (fun, successHandler, errorHandler) {
        openDB();
        dbObj.transaction(fun, errorHandler, successHandler);
    }

    //修改数据库版本信息
    changeDBVersion = function (oldVersion, newVersion) {
        dbObj = openDB();
        dbObj.changeVersion(oldVersion, newVersion, null, errorFun, null);
    }

    //判断某表是否存在:表名、存在回调函数、不存在回调函数
    isExitTable = function (tableName, exitFun, noexitFun) {
        dbObj = openDB();
        var sql = "select * from sqlite_master where type='table' and name = ?";
        dbObj.transaction(function (tx) {
            tx.executeSql(sql, [tableName], function (transaction, result) {
                if (result.rows.length > 0 && exitFun) {
                    exitFun.call();
                } else if (result.rows.length <= 0 && noexitFun) {
                    noexitFun.call();
                }
            }, null);
        });

    }

    //删除表数据:表名,删除成功回调函数
    clearTableData = function (tableName, callBackFun) {
        dbObj = openDB();
        var sql = "delete from ?";
        dbObj.transaction(function (tx) {
            tx.executeSql(sql, [tableName], callBackFun, null);
        });
    }

    //删除表,删除成功回调函数
    deleteTable = function (tableName, callBackFun) {
        dbObj = openDB();
        var sql = "drop table ?";
        dbObj.transaction(function (tx) {
            tx.executeSql(sql, [tableName], callBackFun, null);
        });
    }

}

 

 

你可能感兴趣的:(js,html5,sqllite)