Perl连接SQL Server数据库概述
以下将展示利用Perl语言连接Microsoft SQL Server数据库的具体案例,这些脚本兼容Windows与Linux两种操作系统环境。
Windows平台连接方法
Windows环境下的配置方案
当在Windows系统中执行Perl脚本时,推荐采用基于DBI架构的两个核心模块包,它们能够提供标准化的数据库访问接口。
可选用的驱动包括DBD::ODBC以及DBD::ADO。
应用DBD::ODBC驱动
若决定采用DBD::ODBC作为连接驱动,下述代码片段演示了如何成功接入SQL Server数据库。
复制代码 代码如下: use DBI; # DBD::ODBC my $dsn = 'DBI:ODBC:Driver={SQL Server}'; my $host = '10.0.0.1,1433'; my $database = 'my_database'; my $user = 'sa'; my $auth = ‘s3cr3t'; # Connect via DBD::ODBC by specifying the DSN dynamically. my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, $auth, { RaiseError => 1, AutoCommit => 1} ) || die "Database connection not made: $DBI::errstr"; #Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql ); #Execute the statement $sth->execute(); my( $id, $name, $phone_number ); # Bind the results to the local variables $sth->bind_columns( undef, \$id, \$name, \$phone_number ); #Retrieve values from the result set while( $sth->fetch() ) { print "$id, $name, $phone_number\n"; } #Close the connection $sth->finish(); $dbh->disconnect();
此外,也可以借助预先配置好的系统数据源名称(System DSN)进行连接。创建此类DSN的操作路径为:控制面板 -> 管理工具 -> 数据源。
在使用系统DSN连接时,必须调整连接字符串的格式,具体修改如下所示:
复制代码 代码如下: # Connect via DBD::ODBC using a System DSN my $dbh = DBI->connect("dbi:ODBC:my_system_dsn", $user, $auth, { RaiseError => 1, AutoCommit => 1 } ) || die "Database connection not made: $DBI::errstr";
应用DBD::ADO驱动
假如选择DBD::ADO模块,下列实例展示了连接SQL Server数据库的流程。
复制代码 代码如下: use DBI; my $host = '10.0.0.1,1433'; my $database = 'my_database'; my $user = 'sa'; my $auth = ‘s3cr3t'; # DBD::ADO $dsn = "Provider=sqloledb;Trusted Connection=yes;"; $dsn .= "Server=$host;Database=$database"; my $dbh = DBI->connect("dbi:ADO:$dsn", $user, $auth, { RaiseError => 1, AutoCommit => 1} ) || die "Database connection not made: $DBI::errstr"; #Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql ); #Execute the statement $sth->execute(); my( $id, $name, $phone_number ); # Bind the results to the local variables $sth->bind_columns( undef, \$id, \$name, \$phone_number ); #Retrieve values from the result set while( $sth->fetch() ) { print "$id, $name, $phone_number\n"; } #Close the connection $sth->finish(); $dbh->disconnect();
Linux平台连接方法
Linux环境下的配置方案
若在Linux系统中运行Perl脚本以连接SQL Server,则需要引入DBD::Sybase软件包。
部署SQL Server支持库
Sybase DBD包的正常运行依赖于FreeTDS驱动程序的支持。
FreeTDS的官方下载地址为:www.freetds.org
关于FreeTDS驱动的安装说明文档,请参考链接:http://www.freetds.org/userguide/config.htm
值得注意的是,该驱动方案并未使用ODBC技术。
配置数据源信息
需要编辑freetds.conf配置文件以包含SQL Server数据库的相关信息,配置内容示例如下: 复制代码 代码如下: [SS_MY_DB] host = 10.0.0.1 # or host name port = 1433 tds version = 7.0
安装Sybase DBD模块
该模块的详细文档可查阅:http://search.cpan.org/~mewp/DBD-Sybase/Sybase.pm
同时,必须将sybase环境变量指向FreeTDS的安装目录,例如执行命令:export SYBASE=/usr/local/freetds
Sybase DBI结合SQL Server DSN的使用示例
复制代码 代码如下: # load the DBI module use DBI; use DBD::Sybase; my $database="my_database"; my $user="sa"; my $auth="s3cr3t"; BEGIN { $ENV{SYBASE} = "/usr/local"; } # Connect to the SQL Server Database my $dbh = DBI->connect("dbi:Sybase:server=ss_my_db;database=$database", $user, $auth {RaiseError => 1, AutoCommit => 1} ) || die "Database connection not made: $DBI::errstr"; #Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees"; my $sth = $dbh->prepare( $sql ); #Execute the statement $sth->execute(); my( $id, $name, $phone_number ); # Bind the results to the local variables $sth->bind_columns( undef, \$id, \$name, \$phone_number ); #Retrieve values from the result set while( $sth->fetch() ) { print "$name, $title, $phone\n"; } #Close the connection $sth->finish(); undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase $dbh->disconnect();