javascript封装的sqlite操作类实例

文章出处:网络收集 作者:佚名 发布时间:2015-07-18
这篇文章主要介绍了javascript封装的sqlite操作类,可实现SQLite数据库的初始化及执行SQL语句的功能,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了javascript封装的sqlite操作类。分享给大家供大家参考。具体如下:

function sql(name,v,desc,size,tables){
 this.db=null;
 this.name=name;
 this.v=v;
 this.desc=desc;
 this.size=size;
 this.tables=tables;
 this.ini();
}
sql.prototype.ini=function(){
 var self=this;
 self.db=openDatabase(self.name,self.v,self.desc,self.size);
 self.db.transaction(function(tx){
  self.tables.forEach(function(s){
   tx.executeSql(s,[]);
  });
 });
};
sql.prototype.query=function(sql,opt,rs,err){
 var opt=opt || [];
 var rs =rs || function(){};
 var err=err || function(tx,e){G.alert(e.message);};
 this.db.transaction(function(tx){
  if(typeof(sql)=='object'){
   sql.forEach(function(s){
    tx.executeSql(s,opt,rs,err);
   });
  }else{
   tx.executeSql(sql,opt,rs,err);
  }
 });
};

demo:

var tbs=[
  'CREATE TABLE IF NOT EXISTS cfrids(id varchar(32) PRIMARY KEY,jfs INT,jfx varchar(64),jxx TEXT,ct INT,uinfo TEXT,jia INT,zt INT,bz varchar(16),yue INT)',
  'CREATE INDEX IF NOT EXISTS ct_a ON cfrids(ct)',
  'CREATE TABLE IF NOT EXISTS cliao(id varchar(32) PRIMARY KEY,uid varchar(32),nr TEXT,ct INT,ty varchar(8),ismy INT)',
  'CREATE INDEX IF NOT EXISTS uid_a ON cliao(uid)',
  'CREATE TABLE IF NOT EXISTS czliao(id varchar(32) PRIMARY KEY,nr TEXT,ty varchar(8),ct INT,num INT)'];
  var db=new sql('imdata'+z,'1.0','user data',1048576,tbs);
  db.query('insert into cliao (id,uid,nr,ct,ty,ismy) values (?,?,?,?,?,?)',['afasdf','asdfa','saadf','eeee','rrrr',1]);
  db.query('select * from cliao where uid=? order by ct desc limit ?,10',['22',50],function(tx,rs){
   var l=rs.rows.length;
});