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); }); } }
相关推荐
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23
dragonzht 2020-06-17