建立与数据库的连接

王朝other·作者佚名  2006-11-24
宽屏版  字体: |||超大  

我们使用DBI之前首先需要声明:

#!/usr/bin/perl -w

use DBI;

我们有两种方法可以建立PERL与数据库之间的连接:

#!/usr/bin/perl -w

use DBI;

#建立与数据库的连接,第4个参数标明数据库类型

= DBI->connect( 'connection_string', 'username', 'password', 'mSQL' );

if ( !defined ) {

die "Cannot do \->connect: ::errstr\n";

}

通过这种方法,返回一个数据库句柄。这是一种常用的用法,另外一种方法返回“驱动程序句柄”:

#!/usr/bin/perl -w

use DBI;

= DBI->install_driver( 'mSQL' );

if ( !defined ) {

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()

= DBI->connect( '', , '', );

if ( !defined ) {

die "Cannot connect to mSQL server: ::errstr\n";

}

# Disconnect from the database

->disconnect;

exit;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有