Working with databases

  • 363 views
Uploaded on

This is the eleventh set of slightly updated slides from a Perl programming course that I held some years ago. …

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
363
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Perl Programming Course Working with databasesKrassimir BerovI-can.eu
  • 2. Contents1. DBI2. DBD3. Connect4. Select5. Insert6. Update7. Delete8. Example Application (CGI::Ex::Recipes)
  • 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. DBI• DBI Architecture
  • 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. 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. 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. 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. 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. 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. 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. Example Application
  • 13. DBI• Ressources • Beginning Perl (Chapter 13 – Perl and databases (Introducing Relational Databases)) • perldoc DBI • perldoc DBD::SQLite • perldoc DBD::mysql • etc...
  • 14. Working with databasesQuestions?