Windows和Linux系统下perl连接SQL Server数据库的方法

所属分类: 脚本专栏 / perl 阅读数: 1579
收藏 0 赞 0 分享

本文将提供一些perl连接Microsoft SQL Server数据库的实例。perl脚本运行在Windows和Linux平台。

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

你还可以使用预先设置的一个系统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平台下运行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();

更多精彩内容其他人还在看

使用 use re debug 查看正则表达式的匹配过程

使用 use re 'debug' 查看正则表达式的匹配过程,参见如下的代码
收藏 0 赞 0 分享

perl中的$a和$b介绍

有关perl中的$a和$b,这两个变量是为sort函数准备的内置变量,所以声明时可以不加 my
收藏 0 赞 0 分享

perl用{}修饰变量名的写法分享

在perl中用{}修饰变量名,可以防止 _ 被解释为变量名的一部分
收藏 0 赞 0 分享

Perl使用File::Basename获取文件扩展名的代码

本文为大家介绍的这个例子,实现了获取/home/topgkw中所有文件后缀,其中目录返回空值
收藏 0 赞 0 分享

Perl 哈希Hash用法之入门教程

本文和大家重点讨论一下Perl Hash的用法,哈希是一种数据结构,和数组类似,但是,和数组不同的是,其索引不是数字,而是名字。也就是说,索引(这里,我们将它叫key)不是数字而是任意的唯一的字符串
收藏 0 赞 0 分享

perl哈希的一个实例分析

上一篇文章介绍了hash的入门教程,这篇文章为大家提供一个实例,方便大家深入学习
收藏 0 赞 0 分享

Perl哈希表用法解析

Perl语言有很多值得学习的地方,那么你对Perl哈希表的概念是否熟悉呢,这里和大家分享一下,希望本文的介绍能让你有所收获
收藏 0 赞 0 分享

Perl 哈希的创建和引用介绍

创建,引用仅有两种方法,使用它也是两种,这里简单介绍下, 方便需要的朋友
收藏 0 赞 0 分享

Perl 函数集小结

perl中常用的函数集合,特分享下,方便需要的朋友
收藏 0 赞 0 分享

perl的POD权限问题处理

今天我们继续查找mod_perl对req_header的处理,有需要的朋友建议参考学习之
收藏 0 赞 0 分享
查看更多