aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 16292|回复: 4

aardio以ado sql方式操作excel文件(创建、增加、删除、查询、更新)

[复制链接]

17

主题

69

回帖

1014

积分

四级会员

积分
1014
发表于 2014-5-28 16:16:16 | 显示全部楼层 |阅读模式

  1. class sqlxls {
  2.         ctor(dbPath,...){
  3.             var arg={...};
  4.                 import access;
  5.                 this.db,this.err = access( dbPath);
  6.                 for(i=1;#arg;1){
  7.                         this[arg[i]]=creSql
  8.                 }
  9.         };
  10.        
  11.         createTable = function(shname){
  12.                 if( ! this.db.existsTable(shname) ){
  13.                     var creSql="CREATE TABLE " ++ shname++" ("
  14.                     var tempvalue=0;
  15.                     for(i=1;#arg-1;1){
  16.                             creSql=creSql++arg[i]++" char(20),"
  17.                             tempvalue=i;
  18.                     }
  19.                     creSql=creSql++arg[tempvalue+1]++" char(20))";
  20.                         this.db.exec(creSql);
  21.                 }
  22.         }
  23.        
  24.         add = function(shname,...){
  25.             var arg2={...}
  26.             var sql1="insert into " ++ shname ++" ("
  27.             var tempvalue=0;
  28.             for(i=1;#arg-1;1){
  29.                     sql1=sql1++arg[i]++",";
  30.                     tempvalue=i;
  31.             }
  32.             sql1=sql1++arg[tempvalue+1]++") values("
  33.             var sql2=''
  34.             tempvalue=0;
  35.             for(i=1;#arg-1;1){
  36.                     if(arg2[i]!=null){
  37.                             sql2=sql2++'\''++arg2[i]++'\','
  38.                     }else {
  39.                             sql2=sql2++"null"++','
  40.                     }
  41.                    
  42.                     tempvalue=i;
  43.             }
  44.                 if(arg2[tempvalue+1]!=null){
  45.                         sql2=sql2++'\''++arg2[tempvalue+1]++'\')'
  46.                 }else {
  47.                         sql2=sql2++"null"++')'
  48.                 }
  49.             addSql=sql1++sql2;
  50.                 this.db.exec(addSql);
  51.                 //this.db.close();
  52.         }
  53.        
  54.         del = function(shname,from,to,...){
  55.                 var arg2={...};
  56.                 sql1="Update "++shname++" Set ";
  57.                 var tempvalue=0;
  58.                 for(i=1;#arg-1;1){
  59.                         sql1=sql1++arg[i]++"="++"null"++",";
  60.                         tempvalue=i;
  61.                 }
  62.                 sql1=sql1++arg[tempvalue+1]++"="++"null";
  63.                 var sql2=" Where "
  64.                 tempvalue=0;
  65.                 if( from=to ){
  66.                         if(arg2[to]!=null){
  67.                                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  68.                         }else {
  69.                                 sql2=sql2++arg[to]++"="++"null";
  70.                         }
  71.                 }elseif(from<to){
  72.                         for(i=from;to-1;1){
  73.                                 if(arg2[i]!=null){
  74.                                         sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  75.                                 }else {
  76.                                         sql2=sql2++arg[i]++"="++"null"++' and ';
  77.                                 }
  78.                             tempvalue=i;
  79.                         }
  80.                         if(arg2[tempvalue+1]!=null){
  81.                                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  82.                         }else {
  83.                                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  84.                         }
  85.                 }
  86.                 upsql=sql1++sql2;
  87.                 this.db.exec(upsql)
  88.                 //this.db.close();
  89.         }
  90.        
  91.         update=function(shname,from,to,...){
  92.                 var arg2={...};
  93.                 sql1="Update "++shname++" Set ";
  94.                 var tempvalue=0;
  95.                 for(i=1;#arg-1;1){
  96.                         if(arg2[i]!=null){
  97.                                 sql1=sql1++arg[i]++"="++"'"++arg2[i]++"'"++",";
  98.                         }else {
  99.                                 sql1=sql1++arg[i]++"="++"null"++",";
  100.                         }
  101.                         tempvalue=i;
  102.                 }
  103.                 if(arg2[tempvalue+1]!=null){
  104.                         sql1=sql1++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  105.                 }else {
  106.                         sql1=sql1++arg[tempvalue+1]++"="++"null";
  107.                 }
  108.                 var sql2=" Where "
  109.                 tempvalue=0;
  110.                 if( from=to ){
  111.                         if(arg2[to]!=null){
  112.                                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  113.                         }else {
  114.                                 sql2=sql2++arg[to]++"="++"null";
  115.                         }
  116.                 }elseif(from<to){
  117.                         for(i=from;to-1;1){
  118.                                 if(arg2[i]!=null){
  119.                                         sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  120.                                 }else {
  121.                                         sql2=sql2++arg[i]++"="++"null"++' and ';
  122.                                 }
  123.                             tempvalue=i;
  124.                         }
  125.                         if(arg2[tempvalue+1]!=null){
  126.                                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  127.                         }else {
  128.                                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  129.                         }
  130.                 }
  131.                 upsql=sql1++sql2;
  132.                 this.db.exec(upsql)
  133.                 //this.db.close();
  134.         }
  135.        
  136.         query=function(shname,from,to,...){
  137.                 var arg2={...};
  138.                 sql1="Select * From "++shname;
  139.                 var sql2=" Where "
  140.                 var tempvalue=0;
  141.                 if(from=to){
  142.                         if(arg2[to]!=null){
  143.                                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  144.                         }else {
  145.                                 sql2=sql2++arg[to]++"="++"null";
  146.                         }
  147.                 }elseif(from<to){
  148.                         for(i=from;to-1;1){
  149.                                 if(arg2[i]!=null){
  150.                                         sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  151.                                 }else {
  152.                                         sql2=sql2++arg[i]++"="++"null"++' and ';
  153.                                 }
  154.                             tempvalue=i;
  155.                         }
  156.                         if(arg2[tempvalue+1]!=null){
  157.                                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  158.                         }else {
  159.                                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  160.                         }
  161.                 }       
  162.                 upsql=sql1++sql2;
  163.             import console;
  164.             tempvalue=0;
  165.             var temp=arg[1]++"|";
  166.             for(i=2;#arg-1;1){
  167.                 temp=temp++arg[i]++"|"
  168.                 tempvalue=i;
  169.             }
  170.             temp=temp++arg[tempvalue+1];
  171.             console.log(temp);
  172.             for(rs,fields in this.db.each(upsql,1,3) ){
  173.                 tempvalue=0;
  174.                 temp=rs(arg[1]).value++"|";
  175.                 for(i=2;#arg-1;1){
  176.                     if(rs(arg[i]).value!=null){
  177.                             temp=temp++rs(arg[i]).value++"|"
  178.                     }else {
  179.                             temp=temp++"null"++"|"
  180.                     }
  181.                     tempvalue=i;
  182.                 }
  183.                 if(rs(arg[tempvalue+1]).value!=null){
  184.                         temp=temp++rs(arg[tempvalue+1]).value;
  185.                 }else {
  186.                         temp=temp++"null";
  187.                 }
  188.                 console.log(temp);
  189.                 }
  190.                 //this.db.close();
  191.         }
  192. }

  193. var xls = sqlxls("/考试成绩.xls" //第1个参数:文件路径全名;后面参数为字段名
  194.         ,"学号","姓名","性别","班级","语文","数学","英语");
  195.        
  196. xls.createTable("成绩单");//参数:表名,创建数据表
  197. xls.add("成绩单","2013001","瑞安","男","2013(01)","86","99","89");//第1个参数:表名;后面参数为字段值
  198. xls.add("成绩单","2013002","平阳","女","2013(01)","66","68","68");//第1个参数:表名;后面参数为字段值
  199. xls.add("成绩单","2013003","乐清","女","2013(02)","66","68","68");//第1个参数:表名;后面参数为字段值
  200. xls.add("成绩单","2013004","永嘉","女","2013(02)","66","68","68");//第1个参数:表名;后面参数为字段值
  201. xls.add("成绩单","2013005","永嘉","女","2013(02)","68","76","85");//第1个参数:表名;后面参数为字段值
  202. xls.add("成绩单","2013005","永嘉","男","2013(02)","68","76","85");//第1个参数:表名;后面参数为字段值
  203. xls.update("成绩单",2,3,"2013001","瑞安","男","2013(02)","2","2","2");//第1个参数:表名;第2个参数:从第几列开始匹配;第3个参数:片配到此列;后面参数为全部字段值
  204. xls.del("成绩单",4,4,"2013001","瑞安","女","2013(01)");//第1个参数:表名;第2个参数:从第几列开始匹配;第3个参数:片配到此列;后面参数为全部字段值(除第2个和第3个参数指定范围外,其它字段值可以省略)
  205. xls.query("成绩单",3,4,"2013001","瑞安","女","2013(02)");//第1个参数:表名;第2个参数:从第几列开始匹配;第3个参数:片配到此列;后面参数为全部字段值(除第2个和第3个参数指定范围外,其它字段值可以省略)

复制代码

评分

参与人数 1 +1 收起 理由
未来星 + 1 很给力!

查看全部评分

0

主题

30

回帖

344

积分

培训班

积分
344
QQ
发表于 2014-8-1 14:06:35 | 显示全部楼层
好贴收藏着。

3

主题

6

回帖

60

积分

一级会员

积分
60
发表于 2016-10-11 22:50:49 | 显示全部楼层

3

主题

6

回帖

60

积分

一级会员

积分
60
发表于 2016-12-21 10:05:49 | 显示全部楼层
---------------------------
aardio:运行时错误
---------------------------
按 Ctrl + C复制错误信息
---------------------------------------------------
错误行号:#12
文件:[string "class sqlxls {..."]:
不支持此操作: _get table
定义类型:field(table)
名字:'db'
类型:null
---------------------------------------------------
调用栈:
        [string "class sqlxls {..."]:12: in function 'createTable'
        [string "class sqlxls {..."]:197: in main chunk
---------------------------
确定   
---------------------------

各位大大请教下,我将示例代码中的 xls 改成 xlsx,在一台电脑支持,换了电脑后就是xls支持xlsx不支持弹出这个提示,是否是漏掉什么系统文件没装? 两台电脑excel一个是完整版一个是绿色精简版的,但是精简版功能比较好用不想换完整版。。。

3

主题

6

回帖

60

积分

一级会员

积分
60
发表于 2016-12-21 11:34:07 | 显示全部楼层
本帖最后由 Spirit_8° 于 2016-12-21 11:35 编辑

  1. class sqlxls {
  2.     ctor(dbPath,...){
  3.         var arg={...};
  4.         import access;
  5.         this.db,this.err = access( dbPath);
  6.         for(i=1;#arg;1){
  7.             this[arg[i]]=creSql
  8.         }
  9.     };
  10.      
  11.     createTable = function(shname){
  12.         if( ! this.db.existsTable(shname) ){
  13.             var creSql="CREATE TABLE " ++ shname++" ("
  14.             var tempvalue=0;
  15.             for(i=1;#arg-1;1){
  16.                 creSql=creSql++arg[i]++" char(20),"
  17.                 tempvalue=i;
  18.             }
  19.             creSql=creSql++arg[tempvalue+1]++" char(20))";
  20.             this.db.exec(creSql);
  21.         }
  22.     }
  23.      
  24.     add = function(shname,...){
  25.         var arg2={...}
  26.         var sql1="insert into " ++ shname ++" ("
  27.         var tempvalue=0;
  28.         for(i=1;#arg-1;1){
  29.             sql1=sql1++arg[i]++",";
  30.             tempvalue=i;
  31.         }
  32.         sql1=sql1++arg[tempvalue+1]++") values("
  33.         var sql2=''
  34.         tempvalue=0;
  35.         for(i=1;#arg-1;1){
  36.             if(arg2[i]!=null){
  37.                 sql2=sql2++'\''++arg2[i]++'\','
  38.             }else {
  39.                 sql2=sql2++"null"++','
  40.             }
  41.             
  42.             tempvalue=i;
  43.         }
  44.         if(arg2[tempvalue+1]!=null){
  45.             sql2=sql2++'\''++arg2[tempvalue+1]++'\')'
  46.         }else {
  47.             sql2=sql2++"null"++')'
  48.         }
  49.         addSql=sql1++sql2;
  50.         this.db.exec(addSql);
  51.         //this.db.close();
  52.     }
  53.      
  54.     del = function(shname,from,to,...){
  55.         var arg2={...};
  56.         sql1="Update "++shname++" Set ";
  57.         var tempvalue=0;
  58.         for(i=1;#arg-1;1){
  59.             sql1=sql1++arg[i]++"="++"null"++",";
  60.             tempvalue=i;
  61.         }
  62.         sql1=sql1++arg[tempvalue+1]++"="++"null";
  63.         var sql2=" Where "
  64.         tempvalue=0;
  65.         if( from=to ){
  66.             if(arg2[to]!=null){
  67.                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  68.             }else {
  69.                 sql2=sql2++arg[to]++"="++"null";
  70.             }
  71.         }elseif(from<to){
  72.             for(i=from;to-1;1){
  73.                 if(arg2[i]!=null){
  74.                     sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  75.                 }else {
  76.                     sql2=sql2++arg[i]++"="++"null"++' and ';
  77.                 }
  78.                 tempvalue=i;
  79.             }
  80.             if(arg2[tempvalue+1]!=null){
  81.                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  82.             }else {
  83.                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  84.             }
  85.         }
  86.         upsql=sql1++sql2;
  87.         this.db.exec(upsql)
  88.         //this.db.close();
  89.     }
  90.      
  91.     update=function(shname,from,to,...){
  92.         var arg2={...};
  93.         sql1="Update "++shname++" Set ";
  94.         var tempvalue=0;
  95.         for(i=1;#arg-1;1){
  96.             if(arg2[i]!=null){
  97.                 sql1=sql1++arg[i]++"="++"'"++arg2[i]++"'"++",";
  98.             }else {
  99.                 sql1=sql1++arg[i]++"="++"null"++",";
  100.             }
  101.             tempvalue=i;
  102.         }
  103.         if(arg2[tempvalue+1]!=null){
  104.             sql1=sql1++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  105.         }else {
  106.             sql1=sql1++arg[tempvalue+1]++"="++"null";
  107.         }
  108.         var sql2=" Where "
  109.         tempvalue=0;
  110.         if( from=to ){
  111.             if(arg2[to]!=null){
  112.                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  113.             }else {
  114.                 sql2=sql2++arg[to]++"="++"null";
  115.             }
  116.         }elseif(from<to){
  117.             for(i=from;to-1;1){
  118.                 if(arg2[i]!=null){
  119.                     sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  120.                 }else {
  121.                     sql2=sql2++arg[i]++"="++"null"++' and ';
  122.                 }
  123.                 tempvalue=i;
  124.             }
  125.             if(arg2[tempvalue+1]!=null){
  126.                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  127.             }else {
  128.                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  129.             }
  130.         }
  131.         upsql=sql1++sql2;
  132.         this.db.exec(upsql)
  133.         //this.db.close();
  134.     }
  135.      
  136.     query=function(shname,from,to,...){
  137.         var arg2={...};
  138.         sql1="Select * From "++shname;
  139.         var sql2=" Where "
  140.         var tempvalue=0;
  141.         if(from=to){
  142.             if(arg2[to]!=null){
  143.                 sql2=sql2++arg[to]++"="++"'"++arg2[to]++"'";
  144.             }else {
  145.                 sql2=sql2++arg[to]++"="++"null";
  146.             }
  147.         }elseif(from<to){
  148.             for(i=from;to-1;1){
  149.                 if(arg2[i]!=null){
  150.                     sql2=sql2++arg[i]++"="++"'"++arg2[i]++"'"++' and ';
  151.                 }else {
  152.                     sql2=sql2++arg[i]++"="++"null"++' and ';
  153.                 }
  154.                 tempvalue=i;
  155.             }
  156.             if(arg2[tempvalue+1]!=null){
  157.                 sql2=sql2++arg[tempvalue+1]++"="++"'"++arg2[tempvalue+1]++"'";
  158.             }else {
  159.                 sql2=sql2++arg[tempvalue+1]++"="++"null";
  160.             }
  161.         }   
  162.         upsql=sql1++sql2;
  163.         import console;
  164.         tempvalue=0;
  165.         var temp=arg[1]++"|";
  166.         for(i=2;#arg-1;1){
  167.             temp=temp++arg[i]++"|"
  168.             tempvalue=i;
  169.         }
  170.         temp=temp++arg[tempvalue+1];
  171.         console.log(temp);
  172.         for(rs,fields in this.db.each(upsql,1,3) ){
  173.             tempvalue=0;
  174.             temp=rs(arg[1]).value++"|";
  175.             for(i=2;#arg-1;1){
  176.                 if(rs(arg[i]).value!=null){
  177.                     temp=temp++rs(arg[i]).value++"|"
  178.                 }else {
  179.                     temp=temp++"null"++"|"
  180.                 }
  181.                 tempvalue=i;
  182.             }
  183.             if(rs(arg[tempvalue+1]).value!=null){
  184.                 temp=temp++rs(arg[tempvalue+1]).value;
  185.             }else {
  186.                 temp=temp++"null";
  187.             }
  188.             console.log(temp);
  189.         }
  190.         //this.db.close();
  191.     }
  192. }

  193. var xls = sqlxls("d:/考试成绩.xlsx" //第1个参数:文件路径全名;后面参数为字段名
  194.     ,"学号","姓名","性别","班级","语文","数学","英语");
  195.       console.log(xls);
  196.       console.pause();
复制代码
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-2-10 19:33 , Processed in 0.066378 second(s), 29 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表