Home arrow Perl Programming arrow Page 4 - Creating a Database with Perl and DBI

The SELECT Command - Perl

In this second part of a four-part series on Perl and the DBI, you'll learn how to create a database and how to use a collection of very important commands. This article is excerpted from chapter 15 of the book Beginning Perl by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Creating a Database with Perl and DBI
  2. The DESCRIBE Command
  3. The INSERT Command
  4. The SELECT Command
By: Apress Publishing
Rating: starstarstarstarstar / 8
March 20, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The SELECT command allows us to query the database, and it reports back to us the information that matches the criteria we specify from the table we indicate. We have seen severalSELECTcommands in this form:

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)

ThisSELECTasks for*from the table namedmusicians. The*means “all fields” in the order that they are in the table. We can explicitly ask for the fields by listing them comma separated instead of using the star.

mysql> SELECT player_id, name, phone 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.01 sec)

The fields we select can be in any order.

mysql> SELECT name, phone, player_id FROM musicians;

 name

 phone

 player_id

 Roger Waters

 555-1212 

 1

 Geddy Lee

 555-2323 

 2

 Marshall Mathers III

 555-3434 

 3

 Thom Yorke

 555-4545 

 4

 Lenny Kravitz

 555-5656 

 5

 Mike Diamond

 555-6767 

 6

6 rows in set (0.00 sec)

We can request specific fields—we don’t need to show all the fields available.

mysql> SELECT name, phone FROM musicians;
+----------------------+----------+
| name                 | phone    |
+----------------------+----------+
| Roger Waters         | 555-1212 |
| Geddy Lee            | 555-2323 |
| Marshall Mathers III | 555-3434 |
| Thom Yorke           | 555-4545 |
| Lenny Kravitz        | 555-5656 |
| Mike Diamond         | 555-6767 |
+----------------------+----------+
6 rows in set (0.00 sec)

Please check back next week for the continuation of this article.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: