LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Perl DBI script - No output from program (https://www.linuxquestions.org/questions/linux-newbie-8/perl-dbi-script-no-output-from-program-751704/)

nanda22 09-01-2009 02:44 AM

Perl DBI script - No output from program
 
Hi All,
I've the following code, for reading data from a oracle database table and displaying the columns, but it is not showing any output after excuting, not showing any error also, can anyone guide me where am i making mistake?

Code:

use warnings;
use strict;
use DBI;

my $dbh = DBI->connect('DBI:Oracle:EQPARA', 'sun', 'sunpswd')
                or die "Couldn't connect to database: " . DBI->errstr;


my $sql = qq { SELECT magnitude, phases, depth FROM eqtest };
my $sth = $dbh->prepare ( $sql );
$sth->execute();

my ($magnitude, $phases, $depth );
$sth->bind_columns(undef, \$magnitude, \$phases, \$depth);

while ($sth->fetch()) {
print "$magnitude, $phases, $depth\n";
}

$sth->finish;
         
$dbh->disconnect;


s-tanner 09-02-2009 12:05 AM

I don't know if it makes a difference, but I've always called DBI in this format:

my $dbh = DBI->connect('DBI:Oracle:EQPARA', 'sun', 'sunpswd')
or die "Couldn't connect to database: ", $DBI::errstr;
$dbh->{RaiseError} = 1; # Die if future queries result in an error


Silly question, but are you sure the table has data? Perhaps add these lines after the $sth->execute(); to verify:

my $nf = $sth->{NUM_OF_FIELDS};
print "This query returned $nf fields\n";

PMP 09-02-2009 04:53 AM

Y are you binding the columns,

simple iterate over sth

Code:

while(my $ref = $sth->fetchrow_hashref)
{
  print qq[$ref->{magnitude}, $ref->{phases}, $ref->{depth}];
}



All times are GMT -5. The time now is 10:36 PM.