Perl DBI 操作数据库

  • 连接实例
  • 显示sql结果实例

模块名称

DBI

 

一、连接实例

my $sql = qw{ sql };
my $dbh = DBI->connect("DBI:数据库类型:database=数据库名称;host=localhost","root","passwd", {'RaiseError' => 1});

my $sth=$dbh->prepare("$sql");
$sth->execute();
$sth->finish;

$dbh->disconnect();


my $sql = qw{ sql };

声明要执行的sql语句

 

my $dbh = DBI->connect("DBI:数据库类型:database=数据库名称;host=localhost","root","passwd", {'RaiseError' => 1});

连接数据库,数据库类型为mysql、Oracle、db2.root,passwd对应登录账号密码

 

my $sth=$dbh->prepare("$sql");

准备执行之前声明的sql语句

 

$sth->execute();

执行sql语句

 

$sth->finish;
执行完毕

 

$dbh->disconnect();

断开数据库

 

 

二、显示sql结果实例

my $sql = shift;
my $dbh = DBI->connect("DBI:数据库类型:database=数据库名称;host=localhost","root","passwd", {'RaiseError' => 1});

my $sth=$dbh->prepare("$sql");

$sth->execute();
while (my @now = $sth->fetchrow_array()) {

     print @now;

}
$sth->finish;

$dbh->disconnect();

 

增加一$sth->fetchrow_array()即可

 

更多操作请移步至

http://search.cpan.org/~timb/DBI-1.628/DBI.pm

你可能感兴趣的:(perl)