Perl Programming                 Course            Working with databasesKrassimir BerovI-can.eu
Contents1. DBI2. DBD3. Connect4. Select5. Insert6. Update7. Delete8. Example Application (CGI::Ex::Recipes)
DBI• DBI - Database independent interface for  Perl  • Provides a consistent database interface,    independent of the act...
DBI• DBI Architecture
DBD::*• Drivers provide implementations of the DBI  methods using the private interface functions of  the corresponding da...
Connect• Establishes a database connection, or session, to  the requested $data_source/$dsn.• Returns a database handle ob...
Select• Use Database Handle Methods and  Statement Handle Methods  to retrieve data in various forms  use DBI; use strict;...
Insert• Use prepare() and execute() to insert a  row in a table use DBI; use strict; use warnings; #... my $sql = "INSERT ...
Update• Use prepare() and execute() or do() to  update a row in a table• The do() method can be used for non repeated non-...
Delete• Use prepare() and execute() or do() to  delete a row from a table use DBI; use strict; use warnings; #... my $sql ...
Example Application#1.download and install Apache#2.install CGI::Ex::Recipes within htdocs/#with config option AllowOverri...
Example Application
DBI• Ressources  • Beginning Perl    (Chapter 13 – Perl and databases    (Introducing Relational Databases))  • perldoc DB...
Working with databasesQuestions?
Upcoming SlideShare
Loading in...5
×

Working with databases

495

Published on

This is the eleventh set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
495
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Working with databases

  1. 1. Perl Programming Course Working with databasesKrassimir BerovI-can.eu
  2. 2. Contents1. DBI2. DBD3. Connect4. Select5. Insert6. Update7. Delete8. Example Application (CGI::Ex::Recipes)
  3. 3. DBI• DBI - Database independent interface for Perl • Provides a consistent database interface, independent of the actual database being used • DBI is a layer of "glue" between an application and one or more database driver modules • DBI provides a standard interface and framework for the drivers to operate within • Most of the work is done by the drivers
  4. 4. DBI• DBI Architecture
  5. 5. DBD::*• Drivers provide implementations of the DBI methods using the private interface functions of the corresponding database engine • DBD::mysql – MySQL driver • DBD::SQLite – Self Contained RDBMS • DBD::Oracle - Oracle database driver • DBD::Pg - PostgreSQL database driver perl -MDBI -eprint join $/,DBI->available_drivers, $/ #many available on cpan... cpan[4]> i /^DBD::*/
  6. 6. Connect• Establishes a database connection, or session, to the requested $data_source/$dsn.• Returns a database handle object if the connection succeeds.• Use $dbh->disconnect to terminate the connection.• If the connect fails, it returns undef and sets both $DBI::err and $DBI::errstr. use DBI; use strict; use warnings; my $dsn = dbi:SQLite:dbname=$file; #or my $dsn = DBI:mysql:database=bgcc;host=localhost;; my $dbh = DBI->connect( $dsn, $username, $password ) or die $DBI::errstr;
  7. 7. Select• Use Database Handle Methods and Statement Handle Methods to retrieve data in various forms use DBI; use strict; use warnings; #... my $sql = "SELECT * FROM recipes WHERE id = ?" my $sth = $dbh->prepare($sql); $sth->execute($id); my $hashref = $sth->fetchrow_hashref; my $arrayref = $sth->fetchrow_arrayref; my $arrayref = $sth->fetch; # alias #or just my $arrayref = $dbh->selectrow_arrayref( $sql, %attr, @bind_values); my $hashref = $dbh->selectrow_hashref( $sql, %attr, @bind_values);
  8. 8. Insert• Use prepare() and execute() to insert a row in a table use DBI; use strict; use warnings; #... my $sql = "INSERT INTO recipes (title, problem, analysis) VALUES (?, ?, ?)"; $dbh->prepare($sql)->execute($title,$problem,$analysis);
  9. 9. Update• Use prepare() and execute() or do() to update a row in a table• The do() method can be used for non repeated non- SELECT statement (or with drivers that dont support placeholders) use DBI; use strict; use warnings; #... my $sql = "UPDATE recipes SET title=?, problem=?, analysis=? WHERE id=?"; $true_or_undef = $dbh->prepare($sql)->execute( $title, $problem, $analysis, $id); $rows_affected = $dbh->do($sql_with_hardcoded_values);
  10. 10. Delete• Use prepare() and execute() or do() to delete a row from a table use DBI; use strict; use warnings; #... my $sql = "DELETE FROM recipes WHERE id = ?"; $dbh->prepare($sql)->execute($id); #or just $dbh->do($sql,undef,$id);
  11. 11. Example Application#1.download and install Apache#2.install CGI::Ex::Recipes within htdocs/#with config option AllowOverride Allcpan[1]> install CGI::Ex::Recipes
  12. 12. Example Application
  13. 13. DBI• Ressources • Beginning Perl (Chapter 13 – Perl and databases (Introducing Relational Databases)) • perldoc DBI • perldoc DBD::SQLite • perldoc DBD::mysql • etc...
  14. 14. Working with databasesQuestions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×