本文實例講述了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; });
希望本文所述對大家的javascript程序設計有所幫助。