作者:ycc杨乖乖 | 来源:互联网 | 2023-01-24 14:19
类库优点:
1. 快速建表
2. 封装CURD操作,更加简洁
3. 兼容PhoneGap
4. 自己用着顺手,简化操作
- void function(version){
-
- var app = function(e) {
- _this.DB;
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
- }
-
-
-
-
-
-
- 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;
- });
- });
- }
-
-
-
-
-
-
-
- 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;
- });
- });
- }
-
-
-
-
-
-
-
-
- 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);
- }
-
- }
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
- 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);
-
- }
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- 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');
PhoneGap 的详细介绍:请点这里
PhoneGap 的下载地址:请点这里