Professional Documents
Culture Documents
Raghavedra
Contents
What's DBI?
DBI stands for Database independent interface
for Perl which means DBI provides an
abstraction layer between the Perl code and the
underlying database, allowing you to switch
database implementations really easily.
The DBI is a database access module for the
Perl programming language. It defines a set of
methods, variables, and conventions that
provide a consistent database interface,
independent of the actual database being used.
Database Connection
Assuming we are going to work with Sybase database.
Database Connection
#!/usr/bin/perl
use DBI
use strict;
my $driver = Sybase";
my $database = "TESTDB";
my $userid = "testuser";
my $password = "test123";
my $dbh = DBI->connect ("dbi: $driver :$database",
"$userid", "$password")
or die $DBI::errstr;
Database Connection
If a connection is established with the data
source then a Database Handle is
returned and saved into $dbh for further
use otherwise $dbh is set to undef value
and $DBI:: errstr returns an error string.
INSERT Operation
INSERT operation is required when you want to
create your records into TEST_TABLE.
Executing SQL query to select all the results from the database.
This will be done using execute() API.
Insert Contd..
my $sth = $dbh->prepare ("INSERT INTO
TEST_TABLE (FIRST_NAME, LAST_NAME,
SEX, AGE, INCOME )
values ('john', 'poul ', 'M', 30, 13000)");
$sth->execute() or die $DBI::errstr; $sth>finish();
$dbh->commit or die $DBI::errstr;
READ Operation
READ Operation on any databases means to
fetch some useful information from the
database.
So once our database connection is established,
we are ready to make a query into this database.
Following is the procedure to query all the
records having AGE greater than 20. This will
take four steps
Read Contnd..
Preparing SQL query based on required
Read Contnd..
my $sth = $dbh->prepare ("SELECT FIRST_NAME, LAST_NAME
FROM TEST_TABLE WHERE AGE > 20");
UPDATE Operation
UPDATE Operation on any databases
Update Contd..
Preparing SQL query based on required
Update Contd..
my $sth = $dbh->prepare ("UPDATE
TEST_TABLE SET AGE = AGE + 1 WHERE
SEX = 'M'");
$sth->execute() or die $DBI::errstr;
print "Number of rows updated :" + $sth->rows;
$sth->finish();
$dbh->commit or die $DBI::errstr;
DELETE Operation
DELETE operation is required when you
want to delete some records from your
database.
Delete Contd..
Preparing SQL query based on required
Delete Contd..
$age = 30;
my $sth = $dbh->prepare( "DELETE FROM
TEST_TABLE WHERE AGE = ?");
$sth->execute( $age ) or die $DBI::errstr;
print "Number of rows deleted :" + $sth->rows;
$sth->finish();
$dbh->commit or die $DBI::errstr;
Using do Statement
If you're doing an UPDATE, INSERT, or DELETE there is no data
that comes back from the database, so there is a short cut to
perform this operation.
COMMIT Operation
Commit is the operation which gives a
ROLLBACK Operation
If you are not satisfied with all the changes
and you want to revert back those
changes then use rollback API.
Here is a simple example to call rollback
API.
$dbh->rollback or die $dbh->errstr;
Begin Transaction
Many databases support transactions.
This means that you can make a whole bunch of queries
which would modify the databases, but none of the
changes are actually made.
Then at the end you issue the special SQL query
COMMIT, and all the changes are made simultaneously.
Alternatively, you can issue the query ROLLBACK, in
which case all the queries are thrown away.
Transaction Contd..
begin_work API enables transactions (by
turning AutoCommit off) until the next call to
commit or rollback.
After the next commit or rollback, AutoCommit
will automatically be turned on again.
$rc = $dbh->begin_work or die $dbh->errstr;
AutoCommit Option
If your transactions are simple, you can
Autocommit Contnd..
my $dbh = DBI->connect ($dsn, $userid,
$password, {AutoCommit => 1}) or die
$DBI::errstr;
Here AutoCommit can take value 1 or 0.
Automatic Error
my $dbh = DBI->connect ($dsn, $userid,
$password, {RaiseError => 1}) or die
$DBI::errstr;
Here RaiseError can take value 1 or 0.
Disconnecting Database
To disconnect Database connection, use
disconnect API.
$rc = $dbh->disconnect or warn $dbh>errstr;
Disconnect
The transaction behavior of the disconnect
method is, sadly, undefined.
installed_drivers
%drivers = DBI->installed_drivers();
Returns a list of driver name and driver
handle pairs for all drivers 'installed'
(loaded) into the current process.
The driver name does not include the
'DBD::' prefix.
data_sources
@ary = DBI->data_sources ($driver);
Returns a list of data sources (databases)
available via the named driver.
If $driver is empty or undef, then the value
of the DBI_DRIVER environment variable
is used
errstr
$str = $h->errstr;
or $str = $DBI::errstr
or $str = $h->errstr
Returns the native database engine error
message from the last DBI method called. This
has the same lifespan issues as the "err" method
described above. This is equivalent to
$DBI::errstr or $h->errstr.
rows
$rv = $h->rows;
or $rv = $DBI:: rows
This returns the number of rows effected
by previous SQL statement and equivalent
to $DBI:: rows.
trace
$h->trace( $ trace_settings);
DBI sports an extremely useful ability to generate runtime
tracing information of what it's doing, which can be a
huge time-saver when trying to track down strange
problems in your DBI programs.
You can use different values to set trace level. These
values varies from 0 to 4. The value 0 means disable
trace and 4 means generate complete trace.
THANKS.