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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PERL

Carping About DBI
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek