故障排除perl子程序和dbi错误

时间:2011-07-22 04:04:59

标签: perl dbi subroutine

unable to connect error调用我的第二个子程序。

我正在使用2个子例程,其中主要检查sub 1的结果,如果设置了条件,则运行sub 2。 我可以看到启动sub 2(打印)的值是正确的

两个潜艇都非常相似。我不知道这在哪里失败。

#subs

    sub get_flg
    {
      undef $/;
      open (my $FH, "< l.sql") or die "error can't open this file $!";
      my $sth= $dbh->prepare(<$FH>) ||
          die ("Cannot connect to the database: ".$DBI::errstr."\n");
      $sth->execute;
      close $FH;
      my $row = $sth->fetchrow_hashref;
      $sth->finish;
      return $row->{A};
}

sub get_msg
{
  undef $/;
  open (my $FH, "< 2.sql") or die "error can't open this file $!";
  my $sth= $dbh->prepare(<$FH>) ||
      die ("Cannot connect to the database: ".$DBI::errstr."\n");
  $sth->execute;
  close $FH;
  my $row = $sth->fetchrow_hashref;
  $sth->finish;
  return @$row{'B','C'}; 
}


#MAIN:

my $dbh = DBI->connect($cf{dsn},$cf{user},$cf{pw},
      {AutoCommit => 0, RaiseError => 0, PrintError => 0 }) || die ("Cannot connect to the database: ".$DBI::errstr."\n");
my $val = get_flg();
print $val;
$dbh->disconnect();

if ($val == 0 ) {
    print "$val\n"; 
    } elsif
      ($val == 1) {
    print "My val is $val\n";
    my ($val2,$val3) = get_msg();
    print "$val2:$val3\n";    
    exit;
};
$dbh->disconnect;

1 个答案:

答案 0 :(得分:2)

在调用第一个子例程后,您将与数据库断开连接。

my $val = get_flg();
print $val;
$dbh->disconnect();

删除$dbh->disconnect,你应该没问题。