建立与数据库的连接 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 建立与数据库的连接 (http://www.webasp.net/article/8/7770.htm) |
| -- 作者:未知 -- 发布日期: 2003-09-13 |
| 我们使用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; |
| webasp.net |