Perl
  Home arrow Perl arrow Page 6 - SQL and CGI with Perl and DBI
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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

SQL and CGI with Perl and DBI
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-04-03

    Table of Contents:
  • SQL and CGI with Perl and DBI
  • A More Complex Example
  • Use Placeholders
  • DBI and Table Joins
  • Perl DBI CGI = Fun!
  • What We Didn’t Talk About

  • 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    SQL and CGI with Perl and DBI - What We Didn’t Talk About
    (Page 6 of 6 )

    This chapter is not meant to be an exhaustive discussion of SQL and DBI. There are many topics we did not talk about that should be learned if the maximum power of SQL is to be harnessed.

    First, there are several commands that are essential to use including the following:

    • UPDATE: Allows data in a table to be modified. An example might be

      UPDATE musicians SET phone = "555-9999" WHERE player_id = 3;
    • DELETE: Deletes a row from a table. An example might be 

      DELETE FROM instruments WHERE inst_id = 13;

      Be careful! If the WHERE clause is not used, all rows in the table are deleted.

       
    • REPLACE: If the key provided does not exist, the data is inserted; otherwise the row with that key is first deleted, then the new row is inserted. An example might be

      REPLACE INTO musicians (player_id, name, phone)
             VALUES (1, "Neil Peart", "555-8888");

    In addition to the preceding SQL commands, another topic that is important to know is indexing a table. This can significantly increase the speed ofSELECT statements on large tables. See the docs for more information.

    Speaking of seeing the docs for more information on SQL, as a reminder, be sure to check out the online documentation for MySQL at http://dev.mysql.com/doc/mysql/en/ and the excellent book The Definitive Guide to MySQL, Second Edition by Michael Kofler.

    Summary

    In this chapter we described how we can access a database using Perl and the DBI module. We started with a description of a relational database and followed that with a brief introduction to SQL.

    We then installed MySQL and created a database with three tables. We talked about several SQL commands:INSERTandSELECT were the most important ones. Table joins were discussed as a way to implement the relations in relational databases.

    Then we introducedDBIandDBD::mysql, and wrote several Perl scripts to access and query the database.

    We ended with an example of how easy it is to create dynamic web content by connecting Perl,DBI, andCGI.pm. And in the middle of that discussion we took time out of our busy day to call one of our favorite musicians.

    Exercises

    1. Write a Perl script that prompts the user for an instrument and then prints all the musicians that play that instrument.
    2. Write a CGI program similar to musicians.pl that is a web interface to the script you created for exercise 1.

     


     

    1. These aren’t their real phone numbers. Sorry about that.

    2. www.rush.com

    3. This is a very bad password for many reasons, the least of which is that it is published in this book. For information on creating good passwords, see Hacking Linux Exposed, Second Edition, Brian Hatch, Osborne Press (2002).

    4. This MySQL stuff is easy!

    5. Another bad password, but a snippet of lyric from a great song.

    6. TMTOWTDI in SQL too!

    7. As usual, there are a lot of ways to execute an SQL query and retrieve its results using Perl and DBI. We will look at the easiest and most common way, but you can read about all the various ways by typing
    perldoc DBI at the shell prompt.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Beginning Perl," published by Apress. We...
     

    Buy this book now. This article is excerpted from chapter 15 of the book Beginning Perl (Apress; ISBN: 159059391X). Check it out today at your favorite bookstore. Buy this book now.

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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