欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js+html5操作sqlite數(shù)據(jù)庫的方法

 更新時間:2016年02月02日 11:58:56   作者:皮蛋  
這篇文章主要介紹了js+html5操作sqlite數(shù)據(jù)庫的方法,以完整實例形式分析了JavaScript封裝的html5操作SQLite數(shù)據(jù)庫類,并分析了具體使用技巧,需要的朋友可以參考下

本文實例講述了js+html5操作sqlite數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:

//copyright by lanxyou lanxyou[at]gmail.com
var lanxDB=function(dbname){
  var db=openDatabase(dbname,'1.0.0','',65536);
  return{
    //返回數(shù)據(jù)庫名
    getDBName:function(){
      return dbname;
    },
    //初始化數(shù)據(jù)庫,如果需要則創(chuàng)建表
    init:function(tableName,colums){
      this.switchTable(tableName);
      colums.length>0?this.createTable(colums):'';
      return this;
    },
    //創(chuàng)建表,colums:[name:字段名,type:字段類型]
    createTable:function(colums){
      var sql="CREATE TABLE IF NOT EXISTS " + this._table ;
      var t;
      if (colums instanceof Array && colums.length>0){
        t=[];
        for (var i in colums){
          t.push(colums[i].name+' '+colums[i].type);
        }
        t=t.join(', ');
      }else if(typeof colums=="object"){
        t+=colums.name+' '+colums.type;
      }
      sql=sql+" ("+t+")";
      var that=this;
      db.transaction(function (t) { 
        t.executeSql(sql) ;
   })
    },
    //切換表
    switchTable:function(tableName){
      this._table=tableName;
      return this;
    },
    //插入數(shù)據(jù)并執(zhí)行回調(diào)函數(shù),支持批量插入
    //data為Array類型,每一組值均為Object類型,每一個Obejct的屬性應為表的字段名,對應要保存的值
    insertData:function(data,callback){
      var that=this;
      var sql="INSERT INTO "+this._table;
      if (data instanceof Array && data.length>0){
        var cols=[],qs=[];
        for (var i in data[0]){
          cols.push(i);
          qs.push('?');
        }
        sql+=" ("+cols.join(',')+") Values ("+qs.join(',')+")";
      }else{
        return false;
      }
      var p=[],
        d=data,
        pLenth=0,
        r=[];
      for (var i=0,dLength=d.length;i<dLength;i++){
        var k=[];
        for (var j in d[i]){
          k.push(d[i][j]);
        }
        p.push(k);
      }
      var queue=function(b,result){
        if (result){
          r.push(result.insertId ||result.rowsAffected);
        }
        if (p.length>0){
          db.transaction(function (t) { 
            t.executeSql(sql,p.shift(),queue,that.onfail);
          })
        }else{
          if (callback){
            callback.call(this,r);
          }
        }
      }
      queue();
    },
    _where:'',
    //where語句,支持自寫和以對象屬性值對的形式
    where:function(where){
      if (typeof where==='object'){
        var j=this.toArray(where);
        this._where=j.join(' and ');
      }else if (typeof where==='string'){
        this._where=where;
      }
      return this;
    },
    //更新數(shù)據(jù),data為屬性值對形式
    updateData:function(data,callback){
      var that=this;
      var sql="Update "+this._table;
      data=this.toArray(data).join(',');
      sql+=" Set "+data+" where "+this._where;
      this.doQuery(sql,callback);
    },
    //根據(jù)條件保存數(shù)據(jù),如果存在則更新,不存在則插入數(shù)據(jù)
    saveData:function(data,callback){
      var sql="Select * from "+this._table+" where "+this._where;
      var that=this;
      this.doQuery(sql,function(r){
        if (r.length>0){
          that.updateData(data,callback);
        }else{
          that.insertData([data],callback);
        }
      });
    },
    //獲取數(shù)據(jù)
    getData:function(callback){
      var that=this;
      var sql="Select * from "+that._table;
      that._where.length>0?sql+=" where "+that._where:"";
      that.doQuery(sql,callback);
    },
    //查詢,內(nèi)部方法
    doQuery:function(sql,callback){
      var that=this;
      var a=[];
      var bb=function(b,result){
        if (result.rows.length){
          for (var i=0;i<result.rows.length;i++){
            a.push(result.rows.item(i));
          }
        }else{
          a.push(result.rowsAffected);
        }
        if (callback){
          callback.call(that,a);
        }
      }
      db.transaction(function (t) { 
        t.executeSql(sql,[],bb,that.onfail) ;
      })
    },
    //根據(jù)條件刪除數(shù)據(jù)
    deleteData:function(callback){
      var that=this;
      var sql="delete from "+that._table;
      that._where.length>0?sql+=" where "+that._where:'';
      that.doQuery(sql,callback);
    },
    //刪除表
    dropTable:function(){
      var sql="DROP TABLE IF EXISTS "+this._table;
      this.doQuery(sql);
    },
    _error:'',
    onfail:function(t,e){
      this._error=e.message;
      console.log('----sqlite:'+e.message);
    },
    toArray:function(obj){
      var t=[];
      obj=obj || {};
      if (obj){
        for (var i in obj){
          t.push(i+"='"+obj[i]+"'");
        }
      }
      return t;
    }
  }
}
/*
examples:
var db=new lanxDB('testDB');
db.init('channel_list',[{name:'id',type:'integer primary key autoincrement'},{name:'name',type:'text'},{name:'link',type:'text'},{name:'cover',type:'text'},{name:'updatetime',type:'integer'},{name:'orders',type:'integer'}]);
db.init('feed_list',[{name:'parentid',type:'integer'},{name:'feed',type:'text'}]);
db.switchTable('channel_list').insertData([{name:'aa',link:'ss',updatetime:new Date().getTime()},{name:'bb',link:'kk',updatetime:new Date().getTime()}]);
db.where({name:'aa'}).getData(function(result){
  console.log(result);//result為Array
});
db.where({name:'aa'}).deleteData(function(result){
  console.log(result[0]);//刪除條數(shù)
});
db.where({name:'bb'}).saveData({link:'jj'},function(result){
  console.log(result);//影響條數(shù)
})
})
*/

希望本文所述對大家JavaScript程序設計有所幫助。

相關(guān)文章

最新評論