A very simple example of DBIx::Class
时间:2009-03-30 来源:cobrawgl
From:http://linuxtnt.wordpress.com/2007/08/17/a-very-simple-example-of-dbixclass/
If you don’t have DBIx::Class, you can get it using
$ cpan DBIx::Class
To run the example, first of all we need some directories.
$ mkdir PDB $ mkdir PDB/Main
In the directory PDB, make a file called Main.pm with the following contents:
package PDB::Main; use base qw/DBIx::Class::Schema/; __PACKAGE__->load_classes(qw/Place Person/); 1;
In the directory PDB/Main, make two files, Person.pm
package PDB::Main::Person; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('person'); __PACKAGE__->add_columns(qw/personid name place/); __PACKAGE__->set_primary_key('personid'); 1;
and Place.pm,
package PDB::Main::Place; use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto Core/); __PACKAGE__->table('place'); __PACKAGE__->add_columns(qw/placeid name/); __PACKAGE__->set_primary_key('placeid'); 1;
In the top directory, make a file of SQL statements called persondb.sql as follows:
use persondb; drop table if exists person, place; create table person ( personid mediumint unsigned primary key auto_increment, name varchar(20), place mediumint unsigned references place ); create table place ( placeid mediumint unsigned primary key auto_increment, name varchar(20) );
Now start MySQL:
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 25 Server version: 5.0.38-Ubuntu_0ubuntu1-log Ubuntu 7.04 distribution Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> create database persondb; Query OK, 1 row affected (0.08 sec) mysql> grant all on persondb.* to 'persondb' identified by 'ABCDEFG'; Query OK, 0 rows affected (0.12 sec) mysql> source persondb.sql Database changed Query OK, 0 rows affected, 2 warnings (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql> quit Bye
The last thing is a simple test file called try.pl to run:
#!/usr/bin/perl use warnings; use strict; use PDB::Main; my $schema = PDB::Main->connect("dbi:mysql:persondb", "persondb", "ABCDEFG"); my @people = (['Elvis Presley'], ['Bill Gates'], ['Kate Moss']); $schema->populate('Person', [[qw/name/], @people]); # Search for a member my $elvis = $schema->resultset('Person')->find({name => 'Elvis Presley'}); if ($elvis) { print "Found ", $elvis->name, ", his ID = ", $elvis->personid, "n"; } else { print "Can't find Elvis.n"; } my @places = (["Graceland"], ["Seattle"], ["The Priory"]); $schema->populate('Place', [[qw/name/], @places]); my $graceland = $schema->resultset('Place')->find({name => 'Graceland'}); $elvis->update({place => $graceland->placeid}); if ($elvis->place) { print $elvis->name, " lives in place number ", $elvis->place, ".n"; my $elvis_place = $schema->resultset('Place')->find({placeid => $elvis->place}); if ($elvis_place) { print "The name of this place is ", $elvis_place->name, ".n"; } else { print "This place has no name.n"; } } else { print "Elvis has no place.n"; }
Here is what happened on my computer
$ ./try.pl Found Elvis Presley, his ID = 1 Elvis Presley lives in place number 1. The name of this place is Graceland.
相关阅读 更多 +