DBI:连接所有数据库的API。
DBD:是DBI针对不同数据库的驱动。
DBD::mysql
DBD::Pg
DBD::SQLite
如果没有,使用cpan安装即可。
######################################################################
DBI接口:
习惯用法:
$dbh数据库句柄
$sth语句句柄
$drh驱动句柄
$rc布尔类型返回码
$rv整数类型返回值
@ary从数据库返回的行记录的列表
$rows处理的行数
$fh文件句柄
\%attr哈希类型的属性
@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;
#用connect建立连接返回句柄
$dsn= “DBI:mysql:database=$database;host=$hostname;port=$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(
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;
具体API参考文档。