我有一个使用DBI连接的 Perl 脚本。我使用子例程打开并读取 SQL 脚本文件。我只打印一条记录,我应该再打印两条(总共三条记录)。如何获取所有记录?
结果:
Alert:OUTBOUND_DATA:0
脚本:
my $dbh_oracle = DBI->connect(
$CFG{oracle_dbi_connect},
$CFG{db_user},
$CFG{db_cred},
{AutoCommit => 0,
RaiseError => 0,
PrintError => 0}) or die ("Cannot connect to the database: ".$DBI::errstr."\n");
my ($val1, $val2) = get_data();
print "Alert:$val1:$val2\n";
send_email("Alert:$val1:$val2");
sub get_data
{
undef $/;
open (my $QFH, "< /sql/summary.sql") or die "error can't open this file $!";
my $sth= $dbh_oracle->prepare(<$QFH>) or
die ("Cannot connect to the database: ".$DBI::errstr."\n");
$sth->execute;
close $QFH;
my $row = $sth->fetchrow_hashref;
$sth->finish;
return @$row{'MYTABLE','FLAG'};
}
sub send_email {
my $message = shift;
open (MAIL, "|/usr/sbin/sendmail -t") or die "Can't open sendmail: $!";
print MAIL "To: me\@test.com\n";
print MAIL "From: Data\n";
print MAIL "\n";
print MAIL $message;
close MAIL;
}
exit;
运行查询的结果:(超过 1 个记录)
MYTABLE FLAG
----------------------- ----------
OUTBOUND_DATA 0
MSGS_BY_DIM 0
INBOUND_DATA 0
3 rows selected.