DBI: an APIthat connects all databases.
DBD: is the DBI drive for different databases.
Dbd::mysql
DBD::P G
Dbd::sqlite
If not , install with cpan .
######################################################################
DBI Interface:
Customary usage:
$DBHDatabase Handle
$sthstatement Handle
$DRHDrive Handle
$RCBoolean type return code
$RVinteger type return value
@arylist of row Records returned from the database
$rowsNumber of rows processed
$fhfile Handle
\%attrproperties of the hash type
@driver_names = dbi->available_drivers;
%dirvers = dbi->installed_drivers;
@data_sources = dbi->data_sources ( $driver _name, \%attr);
$dbh = Dbi->connect ($data _source, $username, $auth, \%attr);
###########################################################
Use DBI;
# use connect to make a connection return handle
$dsn= "dbi:mysql:database= $database; host= $hostname;p ort= $port";
$DBH= Dbi->connect ($dsn, $username, $password, {raiseerror = 1, autocommit = 0});
#for Select
$sth = $dbh->prepare ("Select Foo,bar from User WHERE baz=?");
$sth->execute ( $baz);
while (@row = $sth->fetchrow_array) {
print "@row \ n";
"@row \ n";
}
#for non-select (Create,drop,alter,insert,delete)
$sth = $dbh->prepare ("INSERT into table (Foo,bar,baz) VALUES (?,?,?)");
while(<CSV>) {
Chomp;
My ($foo, $bar, $baz) = Split/,/;
($foo, $bar, $baz) = Split/,/;
$sth->execute ($foo, $bar, $baz);
$bar, $baz);
}
$rows _ Affected = $dbh->do (" UPDATE your_table SET foo = foo + 1 ");
$DBH->commit;
$DBH->rollback;
$sth->finish;
$DBH->disconnect;
Specific API reference documentation.
Perl: Database Programming