Sencha Touch 2 结合HTML5的本地存储创建数据库实现增、删、改、查

大家好!我是范范。本人刚接触ST2到现在刚刚两个月,6月1号接的项目,那时才知道有Sencha Touch2这个东西,到现在两个月了期间的幸酸就不说了。今天说说在项目中用到的HTML5的本地存储。可能有的地方不是很合理,大家可以提出宝贵意见。下面开整:


下面是创建数据库的代码:database.js

 

var initDatabase = function(db,tables){



	for(var i=0;i<tables.length;i++){

		pmdb.createTable(db,tables[i],

			function(){ //成功后回调函数

				

			},

			function(tx, error){ //失败后回调函数

			console.log(i);

			}

		);

	}

};



var pmdb =

{

	//建表,在系统初始化时调用

	createTable:function (db,createSql,sucessCallback,failCallBack){

		if(!db) { 

			return ;

		}

		// 创建表  

		db.transaction( function(tx) {   

		    tx.executeSql(  

		        createSql,   //createSql

		        [],   //参数

		        sucessCallback,  //成功后回调函数 

		       	failCallBack     //失败后回调函数

		    );  

		});   

	},



	//执行,

	excute:function (db,sql,param,sucessCallback,failCallBack){

	     if(!db) { 

			return ;

		}

		db.transaction(function(tx) {   

			tx.executeSql(  

		        sql,   

			    param,   

			 	sucessCallback,  //成功后回调函数 

		       	failCallBack     //失败后回调函数

			);   

		}); 

	},



	//删除表

    drop: function(db,tableName) {

     	db.transaction(function (tx) {    

	    	tx.executeSql('DROP TABLE '+tableName);   

		});     

    },

     

	//打开数据库:一个用户一个数据库

	openDB: function (userName){

		var dbName= "PM_"+userName; 

	

		var dbVersion = "1.0" ;

		var dbSize = 1024 * 1024;

		var alia = "工程质量管理数据库";

		var db = openDatabase(dbName, dbVersion, alia, dbSize);   

		return db;

	}

	

	,

	//批量根据id删除数据

	deleteData: function(db,tableName,id,data,sucessCallback,failCallBack){

		if (!db) { 

              return; 

         }

         if(data.length>0){

	         var ids = "";

	         for(var index=0;index<data.length;index++){

	         	if(index==0){

	         		ids="'"+data[index][id]+"'";

	         	}else{

	         		ids+=",'"+data[index][id]+"'";

	         	}

	         }

	         var sql = "delete from " + tableName + " where "+id+" in ("+ids+")";

	         pmdb.excute(db,sql,[],sucessCallback,failCallBack); 

         }

	}

	,

	// 向数据库插入数据 

	insertData : function(db,tableName,field,data,sucessCallback,failCallBack) { 

         if (!db) { 

              return; 

         } 



         for (var index = 0; index < data.length; index++) { 

              var values = data[index]; 

              var value = pmdb.newArray(values, field);   // 将data数据转成Array 

              var fieldnew=newArrayFilds;

              var sql = "INSERT INTO " + tableName + " (" + fieldnew.join(",") 

                       + ") values " + "(" 

                       + new Array(fieldnew.length + 1).join(",?").substr(1) + ")"; 

               

              pmdb.excute(db,sql, value,sucessCallback,failCallBack); 

         }; 

         

         //

     }, 

	// 将data数据转成Array 

	newArray : function(data, filds) { 

         var newArray = new Array(); 

         var k=0;

         for (i = 0; i < filds.length; i++) { 

         	  if(i==0){newArrayFilds.length=0}

              name = filds[i]; 

              var fildData=data[name];

              if(fildData!=undefined&&fildData!=""){

              	if(newArray.length>0){

                newArray[k+1] = fildData;

                newArrayFilds[k+1]=name;

                k=k+1;

              }else{

              	newArray[k] = fildData;

                newArrayFilds[k]=name;

                

              }

             

            }             

         } 

         return newArray; 

     }

    



	

};

var newArrayFilds = new Array();


下面是建表的代码:CreateTable.js

 

 

//建表

var creatTables ={

    sqls :function(){

    	var tables = new Array(2);//表的个数

    	//基础数据表、参数表

	tables[0] = "CREATE TABLE IF NOT EXISTS t_Question  (question_id int, project_id string,category_id int,pj_node_code string,location string,description string,cs_id int,fix_requirement string,fix_date datetime,question_priority string,submit_user_id int,submit_date datetime,question_status int,amount number,IsSubmit int )";
tables[1] = "CREATE TABLE IF NOT EXISTS t_QuestionUser  (question_id string,user_type string,user_id int)";
return tables; } };
 
  

 

 
  
下面是存入时的数据集合:

 

 

var questionArray = new Array("question_id","project_id","category_id","pj_node_code","location","description","cs_id","fix_requirement","fix_date","question_priority","submit_user_id","submit_date","question_status","amount","IsSubmit");
var questionUserArray = new Array("question_id","user_type","user_id");

 




你可能感兴趣的:(Sencha Touch)