This article mainly introduces the javascript encapsulated sqlite operation class, which can realize the function of initializing SQLite database and executing SQL statements. It has some reference value, for more information about the sqlite operation class encapsulated by javascript, see the example in this article. 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.