Home  >  Article  >  Web Front-end  >  Javascript encapsulated sqlite operation class instance_javascript skills

Javascript encapsulated sqlite operation class instance_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:491751browse

The example in this article describes the sqlite operation class encapsulated by javascript. Share it with everyone for your reference. The details are as follows:

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;
});

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn