html5 js 访问 sqlite 数据库的操作类

webkit 核心的浏览器提供了 3个 api接口,用于访问本地sqlite数据,但使用起来很不方便
故而做这个js封装,以方便使用


参考文章:

sqlite API 说明  

http://www.mhtml5.com/resources/html5-js-api%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89-%E6%9C%AC%E5%9C%B0%E6%95%B0%E6%8D%AE%E5%BA%93

另外的操作类 

 http://levi.cg.am/?p=1679

sqlite 语法

http://blog.csdn.net/ejzhang/article/details/6224915


/**
 * js 操作数据库类
 * 
 * @author 肖武 
 */

/**
 * 1、数据库名(mydb)

2、版本号(1.0)

3、描述(Test DB)

4、数据库大小(2*1024*1024) 
 */
var DB = function( db_name, size ){
	 var _db = openDatabase(db_name, '1.0.0','', size );
	 
	 return {
		 
		 /**
		  * 执行sql,回调返回影响条数
		  */
		 execute:function( sql, param, callback ) {
			 //参数处理
			if( !param ){
				param = [];
			}else if(typeof param == 'function' ){
				callback = param;
				param = [];
			}
			 
			 this.query( sql, param, function(result){
				 if( typeof callback == 'function' ){
					 callback(result.rowsAffected);
				 }
			 });
		 },

		 /**
		  * 执行sql,回调返回sql查询对象
		  * 查询时,有数据返回数组,无数据返回0
		  * 增删改时:返回int,影响条数
		  * void query( string[, function])
		  * void query( string[, array[, function]])
		  */
		 query:function(sql, param, callback){
            //参数处理
			if( !param ){
				param = [];
			}else if(typeof param == 'function' ){
				callback = param;
				param = [];
			}
			
			var self=this;
			//只有一个参数
            _db.transaction(function (tx) {
				//4个参数:sql,替换sql中问号的数组,成功回调,出错回调
                tx.executeSql(sql,param,function(tx,result){
					if (typeof callback == 'function' ){
						callback(result);
					}
                },self.onfail) ;
            })
        },
		/**
		 * 插入,回调返回last id
		 * void insert( string, object[, function])
		 */
		insert:function( table, data, callback ){
			if( typeof data != 'object' && typeof callback == 'function' ){
				callback(0);
			}
			
			var k=[];
			var v=[];
			var param=[];
			for(var i in data ){
				k.push(i);
				v.push('?');
				param.push(data[i]);
			}
            var sql="INSERT INTO "+table+"("+k.join(',')+")VALUES("+v.join(',')+")";
			
			this.query(sql, param, function(result){
				if ( typeof callback == 'function' ){
					callback(result.insertId);
				}
			});
		},
		/**
		 * 修改,回调返回影响条数
		 * void update( string, object[, string[, function]])
		 * void update( string, object[, string[, array[, function]]])
		 */
		update:function( table, data, where, param, callback ){
			//参数处理
			if( !param ){
				param = [];
			}else if(typeof param == 'function' ){
				callback = param;
				param = [];
			}
			
			var set_info = this.mkWhere(data);
			for(var i=set_info.param.length-1;i>=0; i--){
				param.unshift(set_info.param[i]);
			}
			var sql = "UPDATE "+table+" SET "+set_info.sql;
			if( where ){
				sql += " WHERE "+where;
			}
			
			this.query(sql, param, function(result){
				if( typeof callback == 'function' ){
					callback(result.rowsAffected);
				}
			});
		},
		
		/**
		 * 删除
		 * void toDelete( string, string[, function]])
		 * void toDelete( string, string[, array[, function]])
		 */
		toDelete:function( table, where, param, callback ){
			//参数处理
			if( !param ){
				param = [];
			}else if(typeof param == 'function' ){
				callback = param;
				param = [];
			}
			
			var sql = "DELETE FROM "+table+" WHERE "+where;
			this.query(sql, param, function(result){
				if( typeof callback == 'function' ){
					callback(result.rowsAffected);
				}
			});
		},
		
		/**
		 * 查询,回调返回结果集数组
		 * void fetch_all( string[, function] )
		 * void fetch_all( string[, param[, function]] )
		 */
		fetchAll:function( sql, param, callback ){
			//参数处理
			if( !param ){
				param = [];
			}else if(typeof param == 'function' ){
				callback = param;
				param = [];
			}
			
			this.query( sql, param, function(result){
				if (typeof callback == 'function' ){
					var out=[];
					
					if (result.rows.length){
						for (var i=0;i









你可能感兴趣的:(html5 js 访问 sqlite 数据库的操作类)