cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks. I have recently set up a new linux server, and and trying to get Perl operational on it. I am trying to run a little test script:
#!/usr/bin/perl -w use lib qw(/home/****/permodules); use SqlSupport; my $dbh = connectpgdb('****','****','****','Pg','localhost'); my $n = getsqlvalue($dbh,"select count(*) from blog"); print $n;
SqlSupport is a helper module I wrote to make db queries easier, and I've been using it for years on my Windows 10 machine. connectpgdb initiates a connection and returns the handle. getsqlvalue returns a single value from a query that should just return one value.

When I run the script it returns without any errors but doesn't print anything. When I run the query from the psql command line client, I get a value. When I run the same script remotely from my windows machine, it prints the same value as returned by the command line client. Just to be sure there's nothing odd going on with the library, I copied the connectpgdb and getsqlvalue subs into the script on the linux machine and it still prints nothing.

I'm kind of flummoxed here. I know the script works and the query produces a result. I don't get how it prints nothing yet there is no connection error or query error.

Replies are listed 'Best First'.
Re: DBD::Pg no errors but empty result?
by choroba (Cardinal) on Mar 18, 2021 at 23:31 UTC
    Are you able to connect using DBI and DBD::Pg?

    Without seeing your helper module, we can only guess. Do you properly check for connection failures?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Well don't I feel stupid. It works. I printed $n rather than "$n\n" so it output the answer then the commend prompt immediately after and I didn't notice.

      Thanks for the response and sorry for the cycles. :-/

      A reply falls below the community's threshold of quality. You may see it by logging in.
      Here's the test script that includes the helper subs. Yes I check for connection failures, whether I'm doing it the right way...
      #!/usr/bin/perl -w use DBI; my $dbh = connectpgdb('****','****','****','Pg','localhost'); my $n = getsqlvalue($dbh,"select count(*) from blog"); print $n; sub connectpgdb { # this is used to connect with DBD::Pg my ($database,$user,$password,$driver,$server) = @_; my $url; unless ($driver) { $driver = "Pg"; } unless ($server) { $url = "DBI:$driver:dbname=$database;host=localhost"; } else { $url = "DBI:$driver:dbname=$database;host=$server;port=54 +32"; } unless ($user) { $user = "****"; $password = "****"; } my $dbh = DBI->connect( $url, $user, $password,{AutoCommit=>1,Rais +eError=>1,PrintError=>1}) or die "connectdb can't connect to psql: $! +\n"; return $dbh; } sub getsqlvalue { my @results = (); my ($dbh,$sqlstatement)= @_; my $sth = $dbh->prepare($sqlstatement); my @row; $sth->execute || die "Could not execute MySQL statement: $sqlstate +ment"; while (@row=$sth->fetchrow_array) { push(@results, [ @row ]); } $sth->finish(); return $results[0][0]; }