perl 獲取表記錄數
阿新 • • 發佈:2019-01-03
#!/usr/bin/perl
#use strict;
use DBI;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);
use Net::SMTP;
sub send_mail{
if (@_ != 2){print "請輸入2個引數\n";exit 1};
($m,$n) = @_; #將引數賦值給變數
my $to_address = $m;
my $CurrTime = time2iso(time());
my $to_address = $m;
my $mail_user = ' [email protected]';
my $mail_pwd = 'xxxxxx';
my $mail_server = 'smtp.exmail.qq.com';
my $from = "From: $mail_user\n";
my $subject = "Subject: zjcap info\n";
my $info = "$CurrTime--$n";
my $message = <<CONTENT;
$info
CONTENT
my $smtp = Net::SMTP->new($mail_server);
$smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!";
$smtp->mail($mail_user);
$smtp->to($to_address);
$smtp->data(); # begin the data
$smtp->datasend($from); # set user
$smtp->datasend($subject); # set subject
$smtp->datasend("\n\n");
$smtp->datasend("$message\n"); # set content
$smtp->dataend();
$smtp->quit();
}
my $dbName = 'oadb';
my $dbUser = 'query';
my $dbUserPass = 'query';
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database ";
my $sql = "select count(*) from VXSPACE.FORMMAIN_1530";
my $sth = $dbh->prepare($sql);
$sth->execute();
my $var = $sth->fetchrow_array();
print "\$var is $var\n";
##讀取上一次檔案
#
open( A, "<", "count.txt" );
while (<A>) {
$count = $_;
};
print "上次記錄的記錄數為$count\n";
###最新的檔案記錄數 寫入count.txt
open( B, ">", "count.txt" );
print B ("$var\n");
if ( defined($count) && $var != $count && $var !=0 ){
print "開始處理\n";
my $sql = "select start_date,field0001 from (select * from vxspace.FORMMAIN_1530 a order by 4 desc) where rownum <2";
my $sth = $dbh->prepare($sql);
$sth->execute();
while (my @arr = $sth->fetchrow_array()){
print "@arr\n";send_mail(' [email protected]',"@arr\n")
}
}