| |||||||||||
| |||||||||||
Adding DBIx on top of DBI and DBD - Select on joined tables
Using SQL Databases from Perl example from a Well House Consultants training course
More on Using SQL Databases from Perl [link]
Source code: D3.pl Module: P308
# Adding DBIx on top of DBI and DBD - Select on joined tables # Pull in the class definition for the database use D3::Main; use strict; # Connect to the already existant database # Individual tables are described within D3::Main my $schema = D3::Main->connect('dbi:SQLite:d3_test.sqlite'); # An easy case of extraction from joined tables # --------------------------------------------- for my $staff ("Graham", "Sarah", "Daniel", "Rachel") { get_computers_by_person($staff); } sub get_computers_by_person { my $person_name = shift; print "Computers looked after by $person_name\n"; my $rs = $schema->resultset('Computers')->search( { 'people.name' => $person_name }, { join => [qw/ people /], } ); while (my $system = $rs->next) { print $system->model . " at " . $system->price . "\n"; } print "\n"; } # Extraction where data is needed from both tables, and sorted # ------------------------------------------------------------ get_all_computers(); sub get_all_computers { print "ALL Computers\n"; my $rs = $schema->resultset('Computers')->search( undef, { join => [qw/ people /], "+select" => [qw/ people.name /], "+as" => [qw /name/ ], "order_by" => { -desc => [qw/ name price /]} } ); while (my $system = $rs->next) { print $system->get_column("name") . " uses ". $system->get_column("model") . " at " . $system->get_column("price") . "\n"; } print "\n"; } __END__ How does this work? ------------------- 1. Set up the database munchkin:pl grahamellis$ sqlite3 d3_test.sqlite SQLite version 3.6.12 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .read D3.sql sqlite> select * from people; 1|Graham 2|Daniel 3|Rachel 4|Sarah sqlite> select * from computers; 1|3|Dell|600 2|2|iPad|400 3|1|Air|1000 4|4|Compaq|640 5|2|Pro|1500 6|1|Pro|1400 sqlite> select * from people join computers on people.pid = computers.pid; 1|Graham|3|1|Air|1000 1|Graham|6|1|Pro|1400 2|Daniel|2|2|iPad|400 2|Daniel|5|2|Pro|1500 3|Rachel|1|3|Dell|600 4|Sarah|4|4|Compaq|640 sqlite> 2. Write the Schema (Main.pm, Computers.pm and People.pm) 3. Run program above -------------------- munchkin:pl grahamellis$ perl D3.pl Computers looked after by Graham Air at 1000 Pro at 1400 Computers looked after by Sarah Compaq at 640 Computers looked after by Daniel iPad at 400 Pro at 1500 Computers looked after by Rachel Dell at 600 ALL Computers Sarah uses Compaq at 640 Rachel uses Dell at 600 Graham uses Pro at 1400 Graham uses Air at 1000 Daniel uses Pro at 1500 Daniel uses iPad at 400 munchkin:pl grahamellis$ Learn about this subject
This module and example are covered on the following public courses:
* MySQL * Perl for Larger Projects Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Using SQL Databases from Perl" training module. You'll find a description of the topic and some
other closely related examples on the "Using SQL Databases from Perl" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |