免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1401 | 回复: 2
打印 上一主题 下一主题

数据查询的脚本,分享一下。 [复制链接]

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-21 13:05 |只看该作者 |倒序浏览
运行代码需要模块,具体如下:
  1. package TDmodule;
  2. use DBI;

  3. sub new{
  4.   $self={};
  5.   $self->{select_db}=undef;
  6.   $self->{delete_db}=undef;
  7.   $self->{insert_db}=undef;
  8.   $self->{update_db}=undef;
  9.   bless $self;
  10.   return $self;
  11. }
  12. sub connect_db{
  13.   my $self=shift;
  14.   if(@_){
  15.     $self->{'connect_db'}=DBI->connect("DBI:mysql:database=@_[0];host=@_[1]","@_[2]","@_[3]") or die "No:$!\n";
  16.   }
  17.   return $self->{'connect_db'};
  18. }
  19. sub select_db{
  20.   my $self=shift;
  21.   if(@_){
  22.       my $select=@_[0]->prepare(@_[1]);
  23.       $select->execute();
  24.       $self{select_db}=$select;
  25.   }
  26.   return $self{select_db};
  27.   @_[0]->disconnect;
  28. }

  29. sub update_db{
  30.   my $self=shift;
  31.   if(@_){
  32.       eval{
  33.         $self->{update_db}=@_[0]->do(@_[1]);
  34.       } or die "No\n";
  35.   }
  36. }

  37. sub insert_db{
  38.   my $self=shift;
  39.   if(@_){
  40.       eval{
  41.         $self->{insert_db}=@_[0]->do(@_[1]);
  42.       } or die "No\n";
  43.   }
  44. }

  45. sub delete_db{
  46.   my $self=shift;
  47.   if(@_){
  48.       eval{
  49.         $self->{delete_db}=@_[0]->do(@_[1]);
  50.       } or die "No\n";
  51.   }
  52. }

  53. 1;
复制代码
先看代码,具体如下:
  1. #!/usr/bin/perl
  2. use TDmodule;

  3. # create object
  4. $test=TDmodule->new();

  5. # connect db return config
  6. $config=$test->connect_db("3d_database","localhost","root","mojige123");

  7. # select for db return database
  8. $old=$test->select_db($config,"select * from old_papar");
  9. $user=$test->select_db($config,"select * from user_papar");

  10. sub result{
  11.   local($sql)=shift;
  12.   local($task)=shift;
  13.   # print execute result
  14.   while($list=$sql->fetchrow_hashref()){
  15.       push(@target,$list->{$task});
  16.   }
  17.   return @target;
  18. }
  19. @old_papar=result($old,'id');

  20. @user_papar=result($user,'number');
  21. foreach my $a (@old_papar){
  22.    print "$a\n";
  23. }
  24. print "--------------\n";
  25. foreach my $b (@user_papar){
  26.    print "$b\n";
  27. }
复制代码
在这种情况下读出的数据会有重复读取的问题,例如目标数据要求是每个表读取一次,但是这个方法看起来是读取一次,实际上是读取了两次。具体结果如下所示:
  1. root@crunchbang:~# perl TDmodule.pl
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. --------------
  8. 2
  9. 4
  10. 6
  11. 8
  12. 10
  13. 145
  14. 693
  15. 168
  16. 746
  17. 206
  18. 319
复制代码
第一次的结果将会包含到第二次中,为了解决这个问题,我仔细的修改了下代码,发现,只需要每次将容器清空,即可。在这分享一下。。。
  1. #!/usr/bin/perl
  2. use TDmodule;

  3. # create object
  4. $test=TDmodule->new();

  5. # connect db return config
  6. $config=$test->connect_db("3d_database","localhost","root","mojige123");

  7. # select for db return database
  8. $old=$test->select_db($config,"select * from old_papar");
  9. $user=$test->select_db($config,"select * from user_papar");

  10. sub result{
  11.   local($sql)=shift;
  12.   local($task)=shift;
  13.   
  14.   # clean list array
  15.   @target=();
  16.   # print execute result
  17.   while($list=$sql->fetchrow_hashref()){
  18.       push(@target,$list->{$task});
  19.   }
  20.   return @target;
  21. }
  22. @old_papar=result($old,'id');

  23. @user_papar=result($user,'number');
  24. foreach my $a (@old_papar){
  25.    print "$a\n";
  26. }
  27. print "--------------\n";
  28. foreach my $b (@user_papar){
  29.    print "$b\n";
  30. }
复制代码
sinian126 该用户已被删除
2 [报告]
发表于 2013-07-21 13:12 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
3 [报告]
发表于 2013-07-21 13:34 |只看该作者
回复 2# sinian126


      好想上海。。。= =
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP