答案:我们使用DBI之前首先需要声明:
#!/usr/bin/perl -w
use DBI;
我们有两种方法可以建立PERL与数据库之间的连接:
#!/usr/bin/perl -w
use DBI;
#建立与数据库的连接,第4个参数标明数据库类型
$dbh = DBI->connect( 'connection_string', 'username', 'password', 'mSQL' );
if ( !defined $dbh ) {
die "Cannot do \$dbh->connect: $DBI::errstr\n";
}
通过这种方法,返回一个数据库句柄。这是一种常用的用法,另外一种方法返回“驱动程序句柄”:
#!/usr/bin/perl -w
use DBI;
$drh = DBI->install_driver( 'mSQL' );
if ( !defined $drh ) {
die "Cannot load driver: $!\n";
}
这种方法多用来检查是否系统中是否存在某种驱动程序。
以下是一些利用DBI处理数据库的历程:
1、打开连接(数据库)已经关闭
#!/usr/bin/perl -w
#
# (c)1996 Alligator Descartes <descarte@hermetica.com>
#
# inout.pl: Connects and disconnects from a specified database
use DBI;
if ( $#ARGV < 0 ) {
die "Usage: inout.pl <Database String> <Database Vendor>\n";
}
# Create new database handle. If we can't connect, die()
$dbh = DBI->connect( '', $ARGV[0], '', $ARGV[1] );
if ( !defined $dbh ) {
die "Cannot connect to mSQL server: $DBI::errstr\n";
}
# Disconnect from the database
$dbh->disconnect;
exit;
上一个:关于odbc的一个文挡说明,奉献给大家
下一个:一个支持HTTP续传下载的PERL程序