Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Perl DBI - returning a single value

by Freezer (Sexton)
on Sep 25, 2012 at 17:29 UTC ( #995600=perlquestion: print w/replies, xml ) Need Help??
Freezer has asked for the wisdom of the Perl Monks concerning the following question:

I feel this should be easy, however I am struggling a bit. The following bit of code is to allow me to take a single count and assign the count to a variable. What am I doing wrong?
my $Count_A_ANY = $dbh_B->selectall_arrayref( "SELECT Count(*) fro +m e_annotation_090812.annotation A WHERE A.user IN (SELECT T.Line FRO +M europhenome_annotation_090812.Temp_table T) AND entity_name LIKE '_ +%' AND evidence_code NOT LIKE '%_________8__' AND A.centre LIKE '".$R +eference."'", { Slice => {} });
I then go on to:
my $Result_summary = "Counts/Summary_table.txt"; open (RESULT_SUMMARY, "+>>$Result_summary") || die "Error opening +outfile.$!,stopped"; if ($loop_count == 1 ){ print RESULT_SUMMARY "Reference center\tComparitor center\tArray p +osition\tKnockout no.\tNo. of agreed parameters\tNo. of hits for refe +rence center A\tD_divided_by_E"; }

Replies are listed 'Best First'.
Re: Perl DBI - returning a single value
by blue_cowdawg (Monsignor) on Sep 25, 2012 at 17:42 UTC
        What am I doing wrong?

    Having written dozens of Perl DBI based scripts/apps/CGI/whatever over the years I'm pretty set in my ways of approaching things like this. So let me show you my method and see if it helps you.

    #!/usr/bin/perl -w # # Boiler-plate-ish database pattern use strict; use DBI; my $dbh = DBI->connect("DBI:Pg:dbname=mydatabase",'','') or die $DBD::errstr; my $sth = $dbh->prepare( qq( SELECT Count(*) as x from e_annotation_090812.annotation A WHERE A.use +r IN (SELECT T.Line FROM europhenome_annotation_090812.Temp_table T) +AND entity_name LIKE '%' AND evidence_code NOT LIKE ? AND A.centre LI +KE ? ) ) or die $dbh->errstr; # Handwaving $ev_code and $pattern2 $sth->execute ("%" . $ev_code,"%" . $pattern2); my ($count) = $sth->fetchrow_array();

    Important elements of that code to consider.

    1. Use placeholders... you'll be glad you did. DBI takes care of making sure the correct quoting is in place. Prepending or appending the wildcard character works well.
    2. The prepare/execute sequence will save you some grief. Especially if you trap errors as I've shown in my example.
    Hope this helps...

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Perl DBI - returning a single value
by CountZero (Bishop) on Sep 25, 2012 at 18:57 UTC
    You will have to do some more reading of the DBI docs.

    They say about selectall_arrayref:

    This utility method combines "prepare", "execute" and "fetchall_arrayref" into a single call. It returns a reference to an array containing a reference to an array (...snip...) for each row of data fetched.
    As your SQL never returns more than one row with one field, you are probably rather looking for selectrow_array:
    This utility method combines "prepare", "execute" and "fetchrow_array" into a single call. If called in a list context, it returns the first row of data from the statement.
    So you code becomes:
    my ($Count_A_ANY) = $dbh_B->selectrow_array( ... your SQL here ... );
    How you link the first part of your code to the second part is a profond mystery to me, so I abstain from commenting.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Perl DBI - returning a single value
by runrig (Abbot) on Sep 25, 2012 at 18:42 UTC
    my $Count_A_ANY = $dbh_B->selectall_arrayref(...

    If you are only fetching one row then:

    my ($Count_A_ANY) = $dbh_B->selectrow_array(...
Re: Perl DBI - returning a single value
by marto (Bishop) on Sep 25, 2012 at 19:22 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995600]
Approved by bulk88
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.
[Lady_Aleena]: So I guess I'll go argue with File::Find and grep.
[Your Mother]: I would probably use Path::Tiny instead for it's slurp and such.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-05-23 19:35 GMT
Find Nodes?
    Voting Booth?