I am facing a rather strange situation. Through a perl script, I connect to the Postgresql database, and I select specific columns from a specific table. The result of the SELECT query from the database is saved in a file. When the script is run a second time, the contents of the file from the first selection are transferred to another file, and the actual file is populated with a new select output. Then I compare these two files. They must be the same at run time twice in the script. The problem is that when I run the script twice to compare, they differ on the same line, when I execute the script a third time, the output will be the same as it was in frist time, and when I execute the fourth time, the output is the same as the second time, and these outputs are repeatedly alternated. I tried 4 different methods how to extract selected rows from a table to a file,but still the same situation, however, the contents of the table in the database remains the same.
See the code below:
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";
my $db = DBI->connect( "dbi:Pg:dbname=<db_name>;host=localhost;","postgres", "*******", {'RaiseError' => 1});
my $qry1s = $db-> prepare("$qry1");
$qry1s->execute();
METHOD 1
while (my @row = $qry1s -> fetchrow_array()) {
$" = ",";
print $out "@row \n";
}
METHOD 2
while (my @row = $qry1s->fetchrow_array()) {
print $out join(", ", @row), "\n"
}
METHOD 3
my ($id,$name);
$qry1s->bind_columns(\($id, $name));
while ($qry1s->fetch){
print $out "$id, $name\n"
}
METHOD 4
my $row;
$row = $qry1s->fetchall_arrayref([]);
foreach my $r (@{$row})
{
print $out join(", ", @{$r}), "\n";
}
In scripts, only one method is uncommented and used, the remaining comments are commented.
$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;
print "Different row/s in new trap list: "."@lonly\n";
};
Outputs>
Actual trap list exists
Files are same
Files are same
2147590214, wlsxStaUnAssociatedFromUnsecureAP
2147590214, wlsxStaUnAssociatedFromUnsecur
Actual trap list exists
Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur
2147590214, wlsxStaUnAssociatedFromUnsecur
2147590214, wlsxStaUnAssociatedFromUnsecureAP
Actual trap list exists
Files are same
Files are same
2147590214, wlsxStaUnAssociatedFromUnsecureAP
2147590214, wlsxStaUnAssociatedFromUnsecur
Actual trap list exists
Files are different
Different row/s in new trap list: 2147590214, wlsxStaUnAssociatedFromUnsecur
2147590214, wlsxStaUnAssociatedFromUnsecur
2147590214, wlsxStaUnAssociatedFromUnsecureAP
Can you help me? :) Thanks
Best wishes Yaro