in reply to DBI prepare() vs do() usage

It depends. If it's a one-shot deal, I (personally) would prefer do() since it's more straightforward, and that's pretty much what it's for. If, however, you plan to do this in a loop, for many updates, so then you'd want to prepare() once, then execute() many times. But, in any case, you should still use placeholders in your do:

#Method 2 ( Use do() ) ---------------------- $dbh = open_dbi(); my $sql = qq{ UPDATE $table{'logins'} SET online='N' WHERE nick=? }); $dbh->do($sql, undef, $nick) or bail_out("Cannot execute UPDATE"); # Close connection
--
3dan

Replies are listed 'Best First'.
Re: Re: DBI prepare() vs do() usage
by kiat (Vicar) on Dec 21, 2003 at 14:22 UTC
    Thanks, edan!

    I didn't know I could use a placeholder with do(). I'll definitely use do() for a one-off execution now that I now it's possible to use a placeholder.

    Btw, why do you need the 'undef' there? Is it compulsory?

      RTFM! At the top:
      $rv = $dbh->do($statement); $rv = $dbh->do($statement, \%attr); $rv = $dbh->do($statement, \%attr, @bind_values);
      Since @bind_values is the 3rd arg and you aren't specifying \%attr, you use either undef or {} in it's place:
      $rv = $dbh->do($statement, undef, @bind_values); $rv = $dbh->do($statement, {}, @bind_values);
      So, then what is \%attr used for? Well, search for that string in the docs and you will be lead to:
         Notation and Conventions
      
         The following conventions are used in this document:
            $dbh    Database handle object
            $sth    Statement handle object
      
            ... yadda yadda ...
      
            undef   NULL values are represented by undefined values in Perl
            \%attr  Reference to a hash of attribute values passed to methods
      
      Do a little more research first, then ask us. :)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)