Perl
  Home arrow Perl arrow Page 4 - Carping About DBI
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Carping About DBI
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 1
    2001-05-02

    Table of Contents:
  • Carping About DBI
  • Dissecting The DBI
  • Animal Antics
  • Do()ing More
  • When Things Go Wrong
  • Speed Demon
  • Dummy Data
  • Croak!
  • Whining Some More
  • Final Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Carping About DBI - Do()ing More


    (Page 4 of 10 )

    Of course, there's a lot more to the DBI than what you've just seen. For example, if you don't like the thought of prepare()-ing and execute()-ing a query, you can take a shortcut with the do() method, used to execute a query directly.

    #!/usr/bin/perl
    # activate module
    use DBI();
    # connect
    my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "me",
    "me545658");
    # execute query
    my $rows = $dbh->do("UPDATE pets SET age = age+1 WHERE name = 'polly'");
    print "$rows row(s) affected\n";
    # clean up
    $dbh->disconnect();
    


    It makes more sense to use do() for SQL statements like CREATE and DROP than to laboriously prepare() and then execute() them. However, if you're going to be running a similar query over and over again, prepare() and execute() will be more efficient. Additionally, do() can't be used for SELECT queries because a statement handle is needed for further data extraction, and do() only returns the number of rows affected by the query (or an undef on error), making it impossible to use for these kinds of operations.

    The rows() method can be used to discover the number of rows affected by the last SQL query. However, don't try to use this after an SQL SELECT query (it's better for INSERTs and DELETEs), because the only way most drivers can figure out the number of rows affected is to actually count them as they are returned. Here's an example:

    #!/usr/bin/perl
    # activate module
    use DBI();
    # connect
    my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "me",
    "me545658", {'RaiseError' => 1});
    # execute query
    my $sth = $dbh->prepare("UPDATE pets SET age=age+1 WHERE name='polly'");
    $sth->execute();
    # print affected rows
    print $sth->rows() . " row(s) affected\n";
    # clean up
    $dbh->disconnect();
    


    If you need special characters quoted correctly, quote() will leap to your rescue.

    #!/usr/bin/perl
    # activate module
    use DBI();
    # connect
    my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "me",
    "me545658", {'RaiseError' => 1});
    # execute query
    $name = $dbh->quote("Godzilla's");
    $dbh->do("INSERT INTO pets VALUES ($name, 'Iguana', 4)");
    # clean up
    $dbh->disconnect();
    


    Finally, the finish() method is used to indicate that no more rows will be processed from the resultset. This allows the database to clean up some of the buffers at its end.

    #!/usr/bin/perl
    # activate module
    use DBI();
    # connect
    my $dbh = DBI->connect("DBI:mysql:database=somedb;host=localhost", "me",
    "me545658", {'RaiseError' => 1});
    # execute query
    my $sth = $dbh->prepare("SELECT * FROM pets");
    // snip
    # clean up
    $sth->finish();
    $dbh->disconnect();
    


    This function is not really required, but it's a good idea to use it if you can. It keeps your code clean, and makes other programmers think you know more than you do, which is always useful.

    This article copyright Melonfire 2001. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PERL ARTICLES

    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway