APICloud开发者进阶之路|[ APP开发技巧 ] Db模块操做封装JS类,超级好用

我封装了一个专门用于操做Db模块的JS类文件。封装的JS类文件以下:
`1. /**sql

  1. * @version 1.0 sqlite操做文件
  2. * @todo 待解决的事情:   采用面向对象实现,这样在拼接SQL的时候就不用受到上次调用一样方法的影响,这样不用再每一个方法都传一个固定的object对象
  3. */
  4. (function(window){
  5.     var u = {};
  6.     u.trim = function(str){
  7.         if(String.prototype.trim){
  8.             return str == null ? "" : String.prototype.trim.call(str);
  9.         }else{
  10.             return str.replace(/(^\s*)|(\s*$)/g, "");
  11.         }
  12.     };
  13.     u.isArray = function(obj){
  14.         if(Array.isArray){
  15.             return Array.isArray(obj);
  16.         }else{
  17.             return obj instanceof Array;
  18.         }
  19.     };
  20.     // 文件操做状态码
  21.     u.fsCode = ['没有错误','找不到文件错误','不可读取错误','编码格式错误','无效操做错误','无效修改错误','磁盘溢出错误','文件已存在错误'];
  22.     u.db = null; // 数据库对象
  23.     u.dbInfo = null; // 存储了是否打开了数据库的相关信息
  24.   
  25.     // 数据库存放在widget的路径,对应要复制到fs目录下的地址固定为fs://sqlite/
  26.     u.dbPath = {"draft" : "widget://res/draft.sqlite"};
  27.     u.fs = null;
  28.     u.storeArr = []; // 用于存放配置对象的数组
  29.     u.idleIds = []; // 空闲已经被释放的optionId
  30.     /**
  31.      * 建立一个用于存放各类属性的对象
  32.      */
  33.     u.createObj = function(){
  34.         if(u.idleIds.length > 0){
  35.             var optionId = u.idleIds.pop();
  36.         }else{
  37.             var optionId = u.storeArr.length;
  38.         }
  39.         
  40.         u.storeArr[optionId] = new Object;
  41.         
  42.         return optionId;
  43.     };
  44.     /**
  45.      * 打开数据库
  46.      * */
  47.     u._openDb = function(optionId,success,fail){
  48.         if(!u.db){
  49.             u.db = api.require('db');
  50.         }
  51.         if (u.storeArr[optionId].dbName == '') {
  52.             console.error('sqlite数据库名称为空');
  53.             return false;
  54.         }
  55.         
  56.         // 获取用户是否已经打开该数据库了
  57.         if(!u.dbInfo){
  58.             u.dbInfo = $api.getStorage('sqlite_dbInfo');
  59.             if(u.dbInfo == undefined){
  60.                 u.dbInfo = {};
  61.             }
  62.         }
  63.         if(u.dbInfo[u.storeArr[optionId].dbName]){
  64.             // 该数据库已经打开了,无需从新打开
  65.             if (typeof success == 'function'){
  66.                 success();
  67.             }
  68.             return true;
  69.         }
  70.         if (!u.fs) {
  71.             u.fs = api.require('fs');
  72.         }
  73.         var filepath = u.getDbPath(u.storeArr[optionId].dbName);
  74.         //先检查fs 有无数据库文件
  75.         u.fs.exist({
  76.             path: filepath
  77.         },function(ret,err){
  78.             if(ret.exist){
  79.                 //找到数据库了 并查询看能用不
  80.                 u._simpleOpenDb(optionId,filepath,success,fail);
  81.             }else{
  82.                 //没有找到数据库 就拷贝一份到fs 目录
  83.                 u.fs.copyTo({
  84.                     oldPath: u.dbPath[u.storeArr[optionId].dbName],
  85.                     newPath: "fs://sqlite"
  86.                 },function(ret,err){
  87.                     if (ret.status) {
  88.                         u._simpleOpenDb(optionId,filepath,success,fail);
  89.                     }else {
  90.                         if(typeof fail == 'function'){
  91.                             if(err.code != undefined){
  92.                                 var msg = u.fsCode[err.code];
  93.                             }else{
  94.                                 var msg = '文件复制失败';
  95.                             }
  96.                             fail(msg);
  97.                         }
  98.                     }
  99.                 });
  100.             }
  101.           });               
  102.     };
  103.     /**
  104.      * 打开数据库
  105.      */
  106.     u._simpleOpenDb = function(optionId,filepath,success,fail){
  107.         // 打开数据库
  108.         u.db.openDatabase({
  109.             name : u.storeArr[optionId].dbName,
  110.             path : filepath
  111.         }, function(ret, err) {
  112.             if(ret.status){
  113.                 u.dbInfo[u.storeArr[optionId].dbName] = true;
  114.                 $api.setStorage('sqlite_dbInfo',u.dbInfo);
  115.                 if(typeof success == 'function'){
  116.                      success();
  117.                 }
  118.             } else {
  119.                 if(typeof fail == 'function'){
  120.                     fail(err.msg);
  121.                 }
  122.             }
  123.         });
  124.     };
  125.     /*
  126.      * 判断某个数据库是否已经打开
  127.      * */
  128.     u.judgeDb = function(dbName){
  129.     };
  130.     /**
  131.      * 关闭数据库
  132.      */
  133.     u.closeDb = function(optionId,callback){
  134.         if(!u.db){
  135.             u.db = api.require('db');
  136.         }
  137.         u.db.closeDatabase({
  138.             name : u.storeArr[optionId].dbName
  139.         }, function(ret, err){
  140.             if(ret.status){
  141.                 u.dbInfo[u.storeArr[optionId].dbName] = true;
  142.                 $api.setStorage('sqlite_dbInfo',u.dbInfo);
  143.                 if(typeof callback == 'function'){
  144.                     callback();
  145.                 }
  146.             }else{
  147.                 api.toast({msg:'关闭数据库'+dbName+'失败!'});
  148.             }
  149.         });
  150.     };
  151.     /**
  152.      *  设置要链接的数据库和数据表
  153.      */
  154.     u.config = function(optionId,tbName,dbName){
  155.         u.storeArr[optionId].tbName = tbName;
  156.         u.storeArr[optionId].dbName = dbName;
  157.         u.storeArr[optionId].fields = '*';
  158.         u.storeArr[optionId].where_str = ''; // where条件语句
  159.         u.storeArr[optionId].order_str = ''; // order by语句
  160.         u.storeArr[optionId].limit_str = '';   // limit 子句
  161.         u.storeArr[optionId].group_str = ''; // group by 子句
  162.         u.storeArr[optionId].last_sql = ''; // 最终的SQL语句
  163.         u.storeArr[optionId].distinct_str = '';
  164.         u.storeArr[optionId].having_str = '';
  165.         
  166.         return this;
  167.     };
  168.     /**
  169.      * 获取数据库对应的fs路径
  170.      */
  171.     u.getDbPath = function(dbName){
  172.         return 'fs://sqlite/'+dbName + '.sqlite';
  173.     };
  174.     /**
  175.      * 执行数据库操做
  176.      */
  177.     u._execute = function(optionId,callback,destroy){
  178.         // 打开数据库
  179.         u._openDb(optionId,function(){
  180.             u.db.executeSql({
  181.                 name: u.storeArr[optionId].dbName,
  182.                 sql: u.storeArr[optionId].last_sql
  183.             }, function(ret, err){
  184.                 if(ret.status){
  185.                     if(undefined == destroy || destroy){
  186.                         u.destoryObj(optionId);
  187.                     }
  188.                     callback && callback(ret.status,'ok');
  189.                 }else{
  190.                     if(err.msg == 'Database Not Open'){
  191.                         // 从新打开一遍数据库
  192.                         var filepath = u.getDbPath(u.storeArr[optionId].dbName);
  193.                         u._simpleOpenDb(optionId,filepath,function(){
  194.                             u.db.executeSql({
  195.                                 name: u.storeArr[optionId].dbName,
  196.                                 sql: u.storeArr[optionId].last_sql
  197.                             }, function(ret, err){
  198.                                 if(undefined == destroy || destroy){
  199.                                     u.destoryObj(optionId);
  200.                                 }
  201.                                 if(ret.status){
  202.                                     callback && callback(ret.status,'ok');
  203.                                 }else{
  204.                                     callback && callback(false,err.msg);
  205.                                 }
  206.                             });
  207.                         },function(){
  208.                             if(undefined == destroy || destroy){
  209.                                 u.destoryObj(optionId);
  210.                             }
  211.                             api.toast({
  212.                                 msg:msg
  213.                             });
  214.                         });
  215.                     }else{
  216.                         if(undefined == destroy || destroy){
  217.                             u.destoryObj(optionId);
  218.                         }
  219.                         callback && callback(false,err.msg);
  220.                     }
  221.                 }
  222.             });
  223.         },function(msg){
  224.             if(undefined == destroy || destroy){
  225.                 u.destoryObj(optionId);
  226.             }
  227.             api.toast({
  228.                 msg:msg
  229.             });
  230.         });
  231.     };
  232.     u._query = function(optionId,callback,destroy){
  233.         // 打开数据库
  234.         u._openDb(optionId,function(){
  235.             u.db.selectSql({
  236.                 name : u.storeArr[optionId].dbName,
  237.                 sql: u.storeArr[optionId].last_sql
  238.             }, function(ret, err){
  239.                 if(ret.status){
  240.                     if(undefined == destroy || destroy){
  241.                         u.destoryObj(optionId);
  242.                     }
  243.                     callback && callback(ret.status,ret.data);
  244.                 }else{
  245.                     if(err.msg == 'Database Not Open'){
  246.                         // 从新打开一遍数据库
  247.                         var filepath = u.getDbPath(u.storeArr[optionId].dbName);
  248.                         u._simpleOpenDb(optionId,filepath,function(){
  249.                             u.db.selectSql({
  250.                                 name: u.storeArr[optionId].dbName,
  251.                                 sql: u.storeArr[optionId].last_sql
  252.                             }, function(ret, err){
  253.                                 if(undefined == destroy || destroy){
  254.                                     u.destoryObj(optionId);
  255.                                 }
  256.                                 if(ret.status){
  257.                                     callback && callback(ret.status,ret.data);
  258.                                 }else{
  259.                                     callback && callback(false,err.msg);
  260.                                 }
  261.                             });
  262.                         },function(){
  263.                             if(undefined == destroy || destroy){
  264.                                 u.destoryObj(optionId);
  265.                             }
  266.                             api.toast({
  267.                                 msg:msg
  268.                             });
  269.                         });
  270.                     }else{
  271.                         if(undefined == destroy || destroy){
  272.                             u.destoryObj(optionId);
  273.                         }
  274.                         callback && callback(false,err.msg);
  275.                     }
  276.                 }
  277.             });
  278.         },function(msg){
  279.             if(undefined == destroy || destroy){
  280.                 u.destoryObj(optionId);
  281.             }
  282.             api.toast({
  283.                 msg:msg
  284.             });
  285.         });
  286.     };
  287.     /**
  288.      * 查询
  289.      */
  290.     u.select = function(optionId,callback,destroy){
  291.         u.storeArr[optionId].last_sql = 'SELECT '+u.storeArr[optionId].distinct_str+u.storeArr[optionId].fields+' FROM `'+u.storeArr[optionId].tbName+'` '+u.storeArr[optionId].where_str + u.storeArr[optionId].group_str + u.storeArr[optionId].having_str + u.storeArr[optionId].order_str + u.storeArr[optionId].limit_str;
  292.         
  293.         u._query(optionId,function(status,data){
  294.             callback && callback(status,data);
  295.         },destroy);
  296.     };
  297.     /**
  298.      * 更新
  299.      * @param 是否销毁对象标识,默认不传为true
  300.      */
  301.     u.save = function(optionId,data,callback,destroy){
  302.         if(undefined == data || !data ){
  303.             console.error('请传递要更新的参数');
  304.             return false;
  305.         }
  306.         
  307.         if (typeof(data) == 'string') {
  308.             var update = data;
  309.         }else if(typeof(data) == 'object'){
  310.             if(u.isArray(data)){
  311.                 console.error('不容许为数组类型');
  312.                 return false;
  313.             }
  314.         
  315.             var str = [],type = '';
  316.             for (var i in data) {
  317.                 type =  typeof(data[i]);
  318.                 switch (type) {
  319.                     case "string" :
  320.                         str.push(i+'="'+u.quotesConvert(data[i])+'"');
  321.                         break;
  322.                     case "object" :
  323.                         if(undefined == data[i].type){
  324.                             console.error(i+'的type参数错误');
  325.                             return false;
  326.                         }
  327.                         if(undefined == data[i].value){
  328.                             console.error(i+'的value参数错误');
  329.                             return false;
  330.                         }
  331.                         if(isNaN(data[i].value)){
  332.                             console.error(i+'的value只容许为数字');
  333.                             return false;                          
  334.                         }
  335.                         data[i].type = data[i].type.toLowerCase();
  336.                         if(data[i].type == 'dec'){
  337.                              str.push(i+'='+i+'-'+data[i].value);
  338.                         }else if(data[i].type == 'inc'){
  339.                              str.push(i+'='+i+'+'+data[i].value);
  340.                         }else{
  341.                             console.error('更新参数'+i+'的type内容只容许为dec或inc');
  342.                             return false;
  343.                         }
  344.                         break;
  345.                      case 'number' :
  346.                         str.push(i+'='+data[i]);
  347.                         break;
  348.                      default :
  349.                         console.error('更新参数'+i+'的内容类型只能为对象、字符串或数字');
  350.                 }
  351.             }
  352.             if(str.length == 0){
  353.                 console.error('更新参数为空');
  354.                 return false;
  355.             }
  356.             var update = str.join(',');
  357.         }else{
  358.             console.error('更新参数类型错误');
  359.             return false;
  360.         }
  361.         u.storeArr[optionId].last_sql = 'UPDATE `'+u.storeArr[optionId].tbName+'` SET '+update+u.storeArr[optionId].where_str + u.storeArr[optionId].order_str + u.storeArr[optionId].limit_str;
  362.         
  363.         u._execute(optionId,function(status,msg){
  364.             callback && callback(status,msg);
  365.         },destroy);
  366.     };
  367.     /**
  368.      * 添加
  369.      * @param obj   option配置选项对象,必须传递
  370.      * @param data 插入的数据,数组形式。
  371.      * @param callback 程序执行结果回调函数
  372.      */
  373.     u.add = function(optionId,data,callback,destroy){
  374.         // 拼接SQL
  375.         if(!u.isArray(data) || data.length == 0){
  376.             console.error('请传递数组或数组为空');
  377.             return false;
  378.         }
  379.         
  380.         var element = data.shift();
  381.         
  382.         if(u.isArray(element) && typeof(element) != 'object'){
  383.             console.error('插入的数据类型必须是json对象');
  384.             return false;
  385.         }
  386.         
  387.         var fields = [];
  388.         var values = [];
  389.         for (var i in element) {
  390.             fields.push(i);
  391.             values.push(u.quotesConvert(element[i]));
  392.         }
  393.         
  394.         var fields_str = fields.join('`,`');
  395.         
  396.         if(!fields_str){
  397.             console.error('数据类型有误');
  398.             return false;
  399.         }
  400.         
  401.         var sql = ' INSERT INTO `'+u.storeArr[optionId].tbName+'`(`'+fields_str+'`) VALUES ("'+values.join('","')+'")';
  402.         
  403.         if (data.length > 0) {
  404.             var val = [],join_str='';
  405.             values=[];
  406.             for (var i = 0; i < data.length;++i) {
  407.                 val = [];   
  408.                 for (var item in data[i]) {
  409.                     val.push(u.quotesConvert(data[i][item]));
  410.                 }
  411.                 join_str = val.join('","');
  412.                 if(join_str){
  413.                     values.push('("'+join_str+'")');
  414.                 }
  415.             }
  416.             u.storeArr[optionId].last_sql = sql +','+values.join(',');
  417.         } else {
  418.             u.storeArr[optionId].last_sql = sql;
  419.         }
  420.         
  421.         // 执行SQL
  422.         u._execute(optionId,function(status,msg){
  423.             callback && callback(status,msg);
  424.         },destroy);
  425.     };
  426.     /**
  427.      * 删除
  428.      */
  429.     u.del = function(optionId,callback,destroy){
  430.         if(u.storeArr[optionId].where_str == ''){
  431.             console.error('请传递where条件');
  432.             return false;
  433.         }
  434.         u.storeArr[optionId].last_sql = 'DELETE FROM `'+u.storeArr[optionId].tbName+'` '+u.storeArr[optionId].where_str + u.storeArr[optionId].order_str + u.storeArr[optionId].limit_str;
  435.         
  436.         u._execute(optionId,function(status,msg){
  437.             callback && callback(status,msg);
  438.         },destroy);
  439.     };
  440.     /**
  441.      * group 子句
  442.      */
  443.     u.group = function(optionId,group){
  444.         if(group){
  445.             u.storeArr[optionId].group_str =  ' GROUP BY '+group+' ';
  446.         }
  447.         
  448.         return this;
  449.     };
  450.     /**
  451.      * distinct
  452.      */
  453.     u.distinct = function(optionId,distinct){
  454.         if(typeof(distinct) == 'boolean' && distinct){
  455.             u.storeArr[optionId].distinct_str = ' DISTINCT ';
  456.         }
  457.     };
  458.     /**
  459.      * having子句
  460.      */
  461.     u.having = function(optionId,having){
  462.         if(typeof(having) == 'string' && having) {
  463.             u.storeArr[optionId].having_str = ' HAVING '+having+' ';
  464.         }
  465.         
  466.         return this;
  467.     }
  468.     /**
  469.      * order子句
  470.      */
  471.     u.order = function(optionId,order){
  472.         if(order){
  473.             u.storeArr[optionId].order_str = ' ORDER BY '+order;
  474.         }
  475.         
  476.         return this;
  477.     };
  478.     /**
  479.      * limit条件
  480.      */
  481.     u.limit = function(optionId,offset,length){
  482.         if (length == undefined) {
  483.             if(offset){
  484.                 u.storeArr[optionId].limit_str = (' LIMIT '+offset);
  485.             }else{
  486.                 u.storeArr[optionId].limit_str = '';
  487.             }
  488.         }else if( undefined != offset){
  489.             u.storeArr[optionId].limit_str = (' LIMIT '+offset+','+length);
  490.         }
  491.         
  492.         return this;
  493.     };
  494.     /**
  495.      * 转换双引号或单引号前加 \ 符号
  496.      */
  497.     u.quotesConvert = function(text,mark){
  498.         if(typeof(text) != 'string' || !text){
  499.             return text;
  500.         }
  501.         
  502.         if(undefined == mark){
  503.             mark = '"';
  504.         }
  505.         
  506.         mark = mark.substr(0,1);
  507.         if(mark == '\\'){
  508.             mark == '\\\\';
  509.         }
  510.         
  511.         if (mark == '"'){
  512.             var wrapper = '\'';
  513.         }else{
  514.             var wrapper = '"';
  515.         }
  516.         var str = 'var tmp=text.replace(/\\'+mark+'/g,'+wrapper+mark+wrapper+');tmp.replace(/'+mark+'/g,'+wrapper+'\\\\'+mark+wrapper+')'
  517.         var result = eval(str);
  518.         return result;
  519.     };
  520.     /*
  521.      *  where条件
  522.      */
  523.     u.where = function(optionId,where){
  524.         var type = typeof(where);
  525.         switch(type){
  526.             case "string" :
  527.                 if(!where){
  528.                     u.storeArr[optionId].where_str = '';
  529.                 }else{
  530.                     u.storeArr[optionId].where_str = ' WHERE '+where;
  531.                 }
  532.                 break;
  533.             case "object" :
  534.                 var str = "";
  535.                 var exeTimes = 0; // 执行次数,用来获取是不是json对象的第一个元素
  536.                 for (var i in where) {
  537.                     i =  u.trim(i);
  538.                     if(!i){
  539.                         continue;
  540.                     }
  541.                     ++exeTimes;
  542.                     if(i == ')'){
  543.                         str += ')';
  544.                         continue;
  545.                     }
  546.                     i = i.replace(/\s+/g,' ');
  547.                     var arr = i.split(' ');
  548.                     if(undefined != arr[0]){
  549.                         arr[0] = arr[0].toLowerCase();
  550.                     }
  551.                     if(undefined != arr[1]){
  552.                         arr[1] = arr[1].toLowerCase();
  553.                     }
  554.                     
  555.                     if(exeTimes == 1 || (arr[0] == ')' || arr[0] == '(' || arr[0] == 'and' || arr[0] == 'or' || arr[1] == 'and' || arr[1] == 'or')) {
  556.                         if(arr[arr.length - 1] == 'like' || arr[arr.length - 1] == '<' || arr[arr.length - 1] == '>' || arr[arr.length - 1] == '=' || arr[arr.length - 1] == '>=' || arr[arr.length - 1] == '<=' || arr[arr.length - 1] == '<>' ) {
  557.                             str += (' '+i);
  558.                         } else {
  559.                             str += (' '+i+'=');
  560.                         }
  561.                     }else{
  562.                         if(arr[arr.length - 1] != undefined){
  563.                             arr[arr.length - 1] = arr[arr.length - 1].toLowerCase();
  564.                         }
  565.                         if(arr[arr.length - 1] == 'like' || arr[arr.length - 1] == '<' || arr[arr.length - 1] == '>' || arr[arr.length - 1] == '=' || arr[arr.length - 1] == '>=' || arr[arr.length - 1] == '<=' || arr[arr.length - 1] == '<>' ) {
  566.                             str += (' AND '+i);
  567.                         } else {
  568.                             str += (' AND '+i+'=');
  569.                         }
  570.                     }
  571.                     if (typeof(where[i]) == 'string') {
  572.                         where[i]  = u.quotesConvert(where[i]);
  573.                         str += ('"'+where[i]+'"');                                       
  574.                     } else if (!isNaN(where[i])) {
  575.                         str += where[i];
  576.                     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  577.                 }
  578.                 if(!str){
  579.                     u.storeArr[optionId].where_str = '';
  580.                 }else{
  581.                     u.storeArr[optionId].where_str = ' WHERE '+str;
  582.                 }
  583.                 break;
  584.             default :
  585.                     console.error('where条件错误');
  586.          }
  587.          
  588.          return this;
  589.     };
  590.     /**
  591.      * 指定要查询的字段
  592.      */
  593.     u.fields = function(optionId,fields){
  594.         if(u.isArray(fields)){
  595.             u.storeArr[optionId].fields = fields.join(',');
  596.         }else if(typeof(fields) == 'string'){
  597.             u.storeArr[optionId].fields = fields;
  598.         }
  599.         
  600.         return this;
  601.     };
  602.     /**
  603.      * SQL语句查询
  604.      */
  605.     u.query = function(optionId,sql,callback,destroy){
  606.         u.storeArr[optionId].last_sql = sql;
  607.         u._query(optionId,function(status,data){
  608.             callback && callback(status,data);
  609.         },destroy);
  610.     };
  611.     /**
  612.      * SQL语句添加。删除、更新操做
  613.      */
  614.     u.exec = function(optionId,sql,callback,destroy){
  615.         u.storeArr[optionId].last_sql = sql;
  616.         u._execute(optionId,function(status,msg){
  617.             callback && callback(status,msg);
  618.         },destroy);
  619.     };
  620.     /**
  621.      *  销毁对象
  622.      */
  623.     u.destoryObj = function(optionId){
  624.         if(undefined != u.storeArr[optionId]){
  625.             u.storeArr[optionId] = null;
  626.             u.idleIds.push(optionId);
  627.         };
  628.     };
  629.     window.$sqlite_api = u;
  630. })(window);

复制代码数据库

用法以下:json

  1.    var sql = 'create table goods(id integer primary key autoincrement,'+
  2. 'name varchar(20),price decimal(10,2),'+
  3. 'shop_id integer, descri varchar(100),'+
  4. 'updatetime datetime)';
  5.    
  6.    // 建立Db对象惟一标识,每次操做都必须建立一个新的Db操做标识,每次操做完成该对象会自动被销毁,每一个方法的第一个参数都必须写这个惟一标识符
  7. var optionId1 = $sqlite_api.createObj();
  8. $sqlite_api.config(optionId1,'','draft').exec(optionId1,sql,function(status,msg){
  9.         if(status){
  10.             alert('sql执行成功');
  11.         }else{
  12.             alert(msg);
  13.         }
  14.    });
  15.    //插入单条数据
  16.    var arr = [{"name":"jim11","age":25,"address":"england","add_time":"2012-01-12 12:12:12"}];
  17.    var optionId2 = $sqlite_api.createObj();
  18. $sqlite_api.config(optionId2,'user','draft').add(optionId2,arr,function(status,msg){
  19.     if(status){
  20.         alert('插入数据成功');
  21.     }else{
  22.         alert('操做失败'+msg);
  23.     }
  24. });
  25.    
  26.    // 插入多条数据
  27.    var arr1 = [{"name":"lucas","age":43,"address":"England","add_time":"2011-01-12 12:12:12"},{"name":"赵老四","age":20,"address":"CHINA","add_time":"2012-01-12 12:12:12"},{"name":"李村品","age":32,"address":"重庆","add_time":"2012-03-12 12:12:12"}];
  28.    var optionId3 = $sqlite_api.createObj();
  29.    $sqlite_api.config(optionId3,'user','draft').add(optionId3,arr1,function(status,msg){
  30.     if(status){
  31.         alert('插入多条数据成功');
  32.     }else{
  33.         alert('多条数据插入失败'+msg);
  34.     }
  35. });
  36. // 读取全表数据
  37. var optionId4 = $sqlite_api.createObj();
  38. $sqlite_api.config(optionId4,'user','draft').select(optionId4,function(status,data){
  39.     if(status){
  40.         alert('读取成功1'+JSON.stringify({"data":data}));
  41.     }else{
  42.         alert('读取失败:失败缘由:'+data);
  43.     }
  44. });
  45. // 读取指定数据
  46. var optionId5 = $sqlite_api.createObj();
  47. $sqlite_api.config(optionId5,'user','draft').where(optionId5,{"name":"lucy","age >":10}).select(optionId5,function(status,data){
  48.     if(status){
  49.         alert('读取成功2'+JSON.stringify({"data":data}));
  50.     }else{
  51.         alert('读取失败:失败缘由:'+data);
  52.     }
  53. });
  54. //var optionId6 = $sqlite_api.createObj();
  55. $sqlite_api.config(optionId6,'user','draft').where(optionId6,{"(name LIKE":"%lucy%","age >":10,") or (name":"张三",")":""}).select(optionId6,function(status,data){
  56.     if(status){
  57.         alert('读取成功3'+JSON.stringify({"data":data}));
  58.     }else{
  59.         alert('读取失败:失败缘由:'+data);
  60.     }
  61. });
  62. //// 读取add_time在2012-01-12 12:12:12以后的用户中年龄最大的用户的姓名和地址
  63. var optionId7 = $sqlite_api.createObj();
  64. $sqlite_api.config(optionId7,'user','draft').order(optionId7,'age desc').limit(optionId7,1).fields(optionId7,['name','address']).where(optionId7,{"add_time >":"2012-01-12 12:12:12"}).select(optionId7,function(status,data){
  65.     if(status){
  66.         alert('读取成功4'+JSON.stringify({"data":data}));
  67.     }else{
  68.         alert('读取失败:失败缘由:'+data);
  69.     }
  70. });
  71. // 更新id=1的用户年龄往上加2,address变动为西安市
  72.     var optionId8 = $sqlite_api.createObj();
  73.     $sqlite_api.config(optionId8,'user','draft').where(optionId8,{"id":1}).save(optionId8,{"age":{"type":"inc","value":2},"address":"西安市"},function(status,msg){
  74.         if(status){
  75.             alert('SQL执行成功');
  76.         }else{
  77.             alert('失败缘由1:'+msg);
  78.         }
  79.     });
  80.    
  81. //  // 验证一下是否是还返回更新成功的提示
  82.     var optionId9 = $sqlite_api.createObj();
  83.     $sqlite_api.config(optionId9,'user','draft').where(optionId9,{"id":1}).save(optionId9,{"age":{"type":"inc","value":2},"address":"西安市"},function(status,msg){
  84.         if(status){
  85.             alert('SQL执行成功');
  86.         }else{
  87.             alert('失败缘由2:'+msg);
  88.         }
  89.     });
  90.    
  91. //  // 更新id=2的用户注册时间为2014-02-12 14:15:14
  92.     var optionId10 = $sqlite_api.createObj();
  93.     $sqlite_api.config(optionId10,'user','draft').where(optionId10,{"id":2}).save(optionId10,{"add_time":"2014-02-12 14:15:14","address":"重庆市","age":{"type":"inc","value":2}},function(status,msg){
  94.         if(status){
  95.             alert('SQL执行成功');
  96.         }else{
  97.             alert('失败缘由:'+msg);
  98.         }
  99.     });
  100.    
  101.     var optionId11 = $sqlite_api.createObj();
  102.     $sqlite_api.config(optionId11,'user','draft').where(optionId11,{"id":2,"or id":1}).select(optionId11,function(status,data){
  103.         if(status){
  104.             alert('SQL执行成功'+JSON.stringify({"data":data}));
  105.         }else{
  106.             alert('失败缘由:'+data);
  107.         }
  108.     });
  109.    
  110.     // 删除id为1的记录
  111.     var optionId12 = $sqlite_api.createObj();
  112.     $sqlite_api.config(optionId12,'user','draft').where(optionId12,{"id":1}).del(optionId12,function(status,msg){
  113.         if(status){
  114.             alert('SQL执行成功');
  115.         }else{
  116.             alert('失败缘由:'+msg);
  117.         }
  118.     });

_复制代码_`api