Javascript-encapsulated sqlite operation class instance, javascriptsqlite

Source: Internet
Author: User

Javascript-encapsulated sqlite operation class instance, javascriptsqlite

This example describes the sqlite operation class encapsulated by javascript. Share it with you 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 help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.