通过Perl脚本

时间:2017-03-03 09:43:51

标签: database postgresql perl

我面对一个很奇怪的情况。通过perl脚本我连接到Postgresql数据库,我从具体表中选择特定的列。来自DB的SELECT查询的输出保存到File中。当第二次执行脚本时,来自第一个选择的文件的内容被移动到另一个文件,并且实际文件被新的选择输出填充。然后我比较这两个文件。它们应该在两次脚本执行时相同。问题是,当我运行两次脚本进行比较时,它们在一行中不同,当我第三次执行脚本时,输出与第一次相同,当我执行第四次时,输出是相同的和第二次一样,这些输出反复交替出现。我尝试了4种不同的方法,如何从表中获取选定的行,但仍然是相同的情况,但是,数据库中表的内容仍然是相同的。

请参阅以下代码:

use strict;
use warnings;
use DBI;
use DBD::Pg;
use File::Copy;
use File::Compare;
use List::Compare;
use MIME::Lite;
use Data::Dumper;
use Sys::Hostname;

my $qry1 = "select id, name from nms_incident_config where enable = 'f' order by name asc";
my $ltraps = '/tmp/traps.list' ;
my $ntraps = '/opt/tools/trapcfg_check/actual_traps.list';

if(-e $ntraps ) {
    print "Actual trap list exists \n";
    move("$ntraps","$ltraps") or die "Copy failed! :$!";
      };

open my $out ,">", "/opt/tools/trapcfg_check/actual_traps.list";

#connect to db and run selects for further use

my $db = DBI->connect( "dbi:Pg:dbname=<db_name>;host=localhost;","postgres", "*******", {'RaiseError' => 1});

my $qry1s = $db-> prepare("$qry1");
$qry1s->execute();

方法1

  while (my @row = $qry1s -> fetchrow_array()) {
     $" = ",";
     print $out "@row \n";
  }

方法2

while (my @row = $qry1s->fetchrow_array()) {
  #print $out "$row[0],$row[1]\n";
  print $out join(", ", @row), "\n"
}

方法3

my ($id,$name);
$qry1s->bind_columns(\($id, $name));
while ($qry1s->fetch){
  print $out "$id, $name\n"
}

方法4

my $row;
$row = $qry1s->fetchall_arrayref([]);
foreach my $r (@{$row})
{
   print $out join(", ", @{$r}), "\n";
}

在脚本中,只有一个方法被取消注释并被使用,其余的被注释。

$db->disconnect();

open F ,"</opt/tools/trapcfg_check/actual_traps.list" or die $!;
open F1 , "/tmp/traps.list" or die $!;

my @ntraps=<F>;
my @ltraps=<F1>;

my @lonly ;
my @nonly ;

   if ( compare( $ltraps,$ntraps ) == 0 ) {
      print "Files are same \n" ;
      die "Files are same\n";
      };

   if ( compare( $ltraps,$ntraps ) ne 0 ) { print "Files are different\n";

      my $tc = List::Compare->new(\@ltraps, \@ntraps );
         @lonly = $tc->get_complement;
#        @nonly = $tc->get_unique;
      print "Different row/s in new trap list: "."@lonly\n";

   };

输出&gt;

# ./tcheck_2.pl
Actual trap list exists

Files are same
Files are same

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# ./tcheck_2.pl
Actual trap list exists

Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# ./tcheck_2.pl
Actual trap list exists

Files are same
Files are same

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# ./tcheck_2.pl
Actual trap list exists

Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /opt/tools/trapcfg_check/actual_traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecur

# cat /tmp/traps.list | grep -i 2147590214
2147590214, wlsxStaUnAssociatedFromUnsecureAP
你能帮帮我吗? :) 感谢

最诚挚的问候 哈罗

1 个答案:

答案 0 :(得分:-1)

As Borodin suggested in the comments,我需要添加

close $out

print循环之后。