1. Home
  2. Computing & Technology
  3. Perl

Adding Object-Relational Mapping to your Perl Application - Part Two
Preparing our Database for Rose::DB

by Kirk Brown
for About.com

Next: Creating a Simple Rose::DB Object

If you recall from an earlier tutorial, we learned about the perl Object-Relational Mapping module called Rose::DB. The next step is to actually create the Perl objects that will represent our database tables. This requires a little shift in the way you think about relational database design, or maybe you're already doing this - it's important to think of each table as an object rather than just a collection of data.

As an example, let's create a simple database table in MySQL that will hold a list of Contacts for a simple address book. We'll call the table contacts and the object itself will be the singular version - contact. I'm assuming here that you already know how to create a simple database, mine is called rose_db_test for this tutorial. If you're looking for further information on MySQL, you might take a look at the PHP/MySQL Guide's How To Create a Database in MySQL Tutorial. Let's create a simple table on our rose_db_test database now and insert some dummy data:

CREATE TABLE `rose_db_test`.`contacts` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL
);
INSERT INTO `rose_db`.`contacts` (
`id` ,
`name` ,
`email`
)
VALUES (
NULL , 'Larry', 'larry@thestooges.com'
), (
NULL , 'Curly', 'curly@thestooges.com'
);
That gives us a few contacts in the database for us to work with, and they should be user ID's 1 & 2 respectively depending on the status of your auto_increment column.

Next we'll take a look at actually setting up the Rose::DB Object to access the data!

Next: Creating a Simple Rose::DB Object

Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Working With Databases
  5. A tutorial on using Rose::DB and Rose::DB::Object to add Object-Relational Mapping to your Perl application.>

©2009 About.com, a part of The New York Times Company.

All rights reserved.