SQLite operation class instance _javascript technique for JavaScript encapsulation

Source: Internet
Author: User
Tags create index sqlite

This example describes the SQLite action class for JavaScript encapsulation. Share to everyone for your reference. Specifically 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 (PRIMARY key,jfs) int,jfx varchar (), jxx text,ct Int,uinfo Text,jia int,zt int,bz varchar (), Yue INT) ',
  ' create INDEX if not EXISTS ct_a on cfrids (CT) ',
  ' Create TABLE if NO T EXISTS Cliao (id varchar (PRIMARY key,uid varchar), nr text,ct int,ty varchar (8), Ismy INT) ',
  ' CREATE INDEX IF NO T EXISTS uid_a on Cliao (UID) ",
  ' CREATE TABLE IF not EXISTS Czliao (id varchar () 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?, ', [' 50],function ' (tx,rs) {
   var l=rs.rows.length ;
});

I hope this article will help you with your JavaScript programming.

Related Article

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.