1

I am learning how to use perl with dbi for oracle. I am simply trying to test getting output, but I am unable to print out the result from this simple select.

I would appreciate if someone can help me figure this out or even show me a better way to write this. Thanks.

use strict;
use File::Basename;
use DBI;
use Time::localtime;
use POSIX qw/uname/;

use vars qw/$dbh $scr $computer_name/;
$scr = basename($0, '');
$computer_name = (uname())[1];

 $dbh = DBI->connect('dbi:Oracle:testdb', 'test', 'test', 
                    {RaiseError => 0, PrintError => 0, AutoCommit => 0}) ||
       die "$scr: connect error on $computer_name [$DBI::errstr]";

my $out = get_val();
print "The date is $out\n";
$dbh->disconnect;

sub get_val
{
  my $sth = $dbh->prepare(q{
        SELECT sysdate 
        FROM   dual}) ||
      die "$scr: prepare error on $computer_name [$DBI::errstr]";
  $sth->execute;
  my $row = $sth->fetchrow_hashref;
  $sth->finish;
  return $row->{VALUE};
   }

2 Answers 2

1

The keys in the hash returned by fetchrow_hashref are the column names. You're getting the key 'VALUE' from the returned hashref, which isn't a column in your query.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, its even case sensitive. It had to be upper case. I had to put SYSDATE
0

fetchrow_hashref() returns each column as a key in the hash. The return statement should be:

return $row->{sysdate};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.