Class::DBI简介
时间:2005-11-01 来源:含笑汉
Class::DBI是对DBI模块的对象化封装,将一些底层的数据库操作封装起来,减少对数据库接口的直接操作。
在Class::DBI以前,使用DBI模块对数据库进行操作。操作代码如下所示:
#!/usr/bin/perl
use DBI;
my $dsn="DBI:mysql:mydata:localhost";
my $db_user="root";
my $db_pass="";
#set database handle $dbh
$dbh=DBI->connect($dsn,$db_user,$db_pass,{RaiseError=>1});
#set string handle $sth
######select from table and print(have data back);
$sth=$dbh->prepare("select * from address");
$sth->execute();
while ( @row = $sth->fetchrow_array ) {
print "@row ";
}
$sth->finish();
######insert into table and print(no data back doesn't need $str->finish();
$sth=$dbh->prepare("insert into address(id,name,email,telephone) values(2,'test2','[email protected]',12345678)");
$sth->execute();
$dbh->disconnect();
每次对数据库进行操作都要进行数据库的连接,SQL语句的写入,连接的关闭等。
而Class::DBI将这些底层的数据库操作进行一系列的封装,并提供一些简单的接口,用户只需继承Class::DBI就可使用这些接口,进行数据库操作。
Class::DBI的使用范例请参考:dbi/Classdbi下的响应模块。
Class::DBI的接口说明请参考模块源码及dbi/Classdbi下的文档。
还可参考文档:http://www.1313s.com/f/zt_Class_DBI.html
http://www.perlmonks.org/index.pl?node_id=279077