4
我面臨一個很奇怪的情況。通過perl腳本我連接到Postgresql數據庫,我從具體表中選擇特定的列。來自數據庫的SELECT查詢的輸出被保存到文件中。當腳本第二次執行時,來自第一個選擇的文件內容將被移動到另一個文件,並且實際文件將被填充新的選擇輸出。然後我比較這兩個文件。在執行兩次腳本時它們應該是相同的。問題是,當我運行腳本兩次比較時,它們在一行中不同,當我第三次執行腳本時,輸出與第一次執行時相同,當我執行第四次時,輸出是相同的如同第二次,這些輸出反覆交替。我嘗試了4種不同的方法,如何從表中將選定的行提取到文件中,但仍然是相同的情況,但是,數據庫中表的內容仍然相同。通過Perl腳本從Postgresql數據庫表中選擇不同的輸出
見下面的代碼:
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";
};
輸出>
# ./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
你能幫助我嗎? :) 感謝
問候 哈羅
您的程序是否還有比您展示的更多內容?輸出中缺少「eAP」的行是否在文件末尾?在'print'循環之後嘗試'關閉$ out'。 – Borodin
你應該設置'PrintError => 0'以及'RaiseError => 1';你不需要警告以及錯誤都會說同樣的事情。 – Borodin
你確定'nms_incident_config'是一個常規表而不是視圖嗎? – ThisSuitIsBlackNot