开发者

perl - help fetching an additional column with dbi call

开发者 https://www.devze.com 2023-03-21 06:27 出处:网络
How do I grab another column from a fetchrow_hashref? For example, my original sub routine returned one value.

How do I grab another column from a fetchrow_hashref?

For example, my original sub routine returned one value.

 sub get_val
    {
      undef $/;
      open (my $FH, "< tst.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->{COL1};
      print $row;
    }

I altered my sql, so now I'm getting 2 values in return and want to fetch COL2

COL1     COL2
------   -------开发者_如何学JAVA
1        A

Here is where I call by get_val:

my $dbh = DBI->connect(
      $abc{oracle_dbi_connect},
      $abc{usr},
      $abc{pw},
      {AutoCommit => 0,RaiseError => 0, PrintError => 0}) || die ("Cannot connect to the database: ".$DBI::errstr."\n");
my $val = get_val();
$dbh->disconnect();

I probably would then want to assign COL2 results to a new $val2?


I'm sure getting COL2 is as simple as using $row->{COL2} instead of $row->{COL1} (as you had in your code). So, make your get_val function return two values:

return @$row{'COL1', 'COL2'};

and in the calling function:

my ($val, $val2) = get_val();
0

精彩评论

暂无评论...
验证码 换一张
取 消