Perl: Database Programming

Source: Internet
Author: User
Tags dsn

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.