Perl
  Home arrow Perl arrow Page 3 - Creating a Database with Perl and 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

Creating a Database with Perl and DBI
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-03-20


    Table of Contents:
  • Creating a Database with Perl and DBI
  • The DESCRIBE Command
  • The INSERT Command
  • The SELECT Command

  • 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


    Creating a Database with Perl and DBI - The INSERT Command
    ( Page 3 of 4 )

    Now comes the time to insert data into the table. We will use the SQL command named INSERT. The basic syntax of this command is

    INSERT INTO table_name (field1, field2, ...) VALUES (value1, value2, ...);

    We first tell MySQL into what table we are inserting a row of data. Then, within parentheses, we indicate which fields in the table will be given values. The second set of parentheses after the term VALUES contains a list of values that are plugged in memberwise into the fields indicated in the first set of parentheses.

    Roger Waters is deserving of a row of data in our table, so let’s insert him as key 1, including his phone number:

    mysql> INSERT INTO musicians (player_id, name, phone)
       -> VALUES (1, "Roger Waters", "555-1212");
    Query OK, 1 row affected (0.01 sec)

    The SELECT command can tell us if the row was inserted correctly (more on SELECT later).

    mysql> SELECT * FROM musicians;
    +-----------+--------------+----------+
    | player_id | name         | phone    |
    +-----------+--------------+----------+
    |         1 | Roger Waters | 555-1212 |
    +-----------+--------------+----------+
    1 row in set (0.00 sec)

    Let’s enter the other musicians:

    mysql> INSERT INTO musicians (player_id, name, phone)
    -> VALUES (2, "Geddy Lee", "555-2323");
    Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO musicians (player_id, name, phone)
    -> VALUES (3, "Marshall Mathers III", "555-3434");
    Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO musicians (player_id, name, phone)
    -> VALUES (4, "Thom Yorke", "555-4545");
    Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO musicians (player_id, name, phone)
    -> VALUES (5, "Lenny Kravitz", "555-5656");
    Query OK, 1 row affected (0.00 sec)

    mysql> INSERT INTO musicians (player_id, name, phone)
    -> VALUES (6, "Mike Diamond", "555-6767");
    Query OK, 1 row affected (0.00 sec)

    mysql> SELECT * FROM musicians;

     player_id

     name

     phone

     1

     Roger Waters

     555-1212 

     2

     Geddy Lee

     555-2323 

     3

     Marshall Mathers III

     555-3434 

     4

     Thom Yorke

     555-4545 

     5

     Lenny Kravitz

     555-5656 

     6

     Mike Diamond

     555-6767 

    6 rows in set (0.00 sec)

     

    Excellent! Our musicians are entered. Now for the commands to enter data into the other two tables. Read along and follow the bouncing ball . . .

    mysql> INSERT INTO what_they_play (player_id, inst_id)
       -> VALUES (1, 11), (1, 14), (2, 12), (2, 14), (3, 14),
       -> (4, 7), (4, 11), (4, 14), (5, 11), (5, 14), (6, 9);
    Query OK, 11 rows affected (0.00 sec) Records: 11  Duplicates: 0 Warnings: 0 

    mysql> SELECT * FROM what_they_play;
    +-------------+----------+
    | player_id  | inst_id  |
    +-------------+----------+
    |              1  |        11 |
    |              1  |        14 |
    |              2  |        12 |
    |              2  |        14 |
    |              3  |        14 |
    |              4  |          7 |
    |              4  |        11 |
    |              4  |        14 |
    |              5  |        11 |
    |              5  |        14 |
    |              6  |          9 |
    +-------------+----------+
    11 rows in set (0.00 sec)

    Notice that we used an alternative form of the INSERT command to insert multiple rows, in our case all the rows, at the same time.

    mysql> INSERT INTO instruments
       
    ->  (inst_id, instrument, type, difficulty)
       
    ->  VALUES
       
    ->  (1, "bagpipes", "reed", 9),
       
    ->  (2, "oboe", "reed", 9),
       
    ->  (3, "violin", "string", 7),
       
    ->  (4, "harp", "string", 8),
       
    ->  (5, "trumpet", "brass", 5),
       
    ->  (6, "bugle", "brass", 6),
       
    ->  (7, "keyboards", "keys", 1),
       
    ->  (8, "timpani", "percussion", 4),
       
    ->  (9, "drums", "percussion", 0),
       
    ->  (10, "piccolo", "flute", 5),
       
    ->  (11, "guitar", "string", 4),
       
    ->  (12, "bass", "string", 3),
       
    ->  (13, "conductor", "for-show-only", 0),
       
    ->  (14, "vocals", "vocal", 5);
    Query OK, 14 rows affected (0.00 sec) Records: 14  Duplicates: 0 Warnings: 0

    mysql> SELECT * FROM instruments;

     inst_id

     instrument type

     difficulty

     1 bagpipes

     reed

     9

     2 oboe

     reed

     9

     3 violin

     string

     7

     4 harp

     string

     8

     5 trumpet

     brass

     5

     6 bugle

     brass

     6

     7 keyboards

     keys

     1

     8 timpani

     percussion

     4

     9 drums

     percussion

     0

     10 piccolo

     flute

     5

     11 guitar

     string

     4

     12 bass

     string

     3

     13 conductor

     for-show-only

     0

     14 vocals

     vocal

     5

    14 rows in set (0.00 sec)

    Now that the three tables have been created and populated with data, we can talk about how we can pull information out of the database.



     
     
    >>> More Perl Articles          >>> More By Apress Publishing
     

       

    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