Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
      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

In reply to Re: Perl DBI - returning a single value by blue_cowdawg
in thread Perl DBI - returning a single value by Freezer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2014-10-24 12:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (131 votes), past polls