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

所属分类: 脚本专栏 / perl 阅读数: 1569
收藏 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();

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

perl 文件操作总结

perl 文件操作总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Perl的经典用法分享

Perl的经典用法分享,学习perl的朋友可以参考下
收藏 0 赞 0 分享

perl文件操作的一些例子分享

有关perl文件操作的一些例子,供大家学习参考
收藏 0 赞 0 分享

perl 中的or与||的区别

or比||优先级低,除此之外,两者无区别
收藏 0 赞 0 分享

Perl中use和require用法对比分析

Perl 中的use和require,都是用来加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域内的化,用Perl use是不可以引入的
收藏 0 赞 0 分享

Perl合并文本的一段实例代码

Perl合并文本的一段实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Perl 文本文件的读写操作、文件的重命名和删除、多个文本文件的合并实现代码

Perl 文本文件的读写操作、文件的重命名和删除、多个文本文件的合并实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

使用Perl创建指定编码格式(如utf-8)文件的实现代码

当Perl读入的源文件是Unicode的utf-8格式时,在使用Perl处理并输出到一个新文件以后,编码格式会自动发生变化
收藏 0 赞 0 分享

perl文件读取的几种处理方式小结

perl打开文件后,可以有几种方式对文件进行读取处理(根据文件大小,文件内容的特征和其它要处理的方式)
收藏 0 赞 0 分享

perl调用外部命令(g)awk的方法

perl为行读取方式,有着强大的对行处理的能力,同样,通过内部命令和巧妙的编程技巧,其对列的处理能力同样不可小觑
收藏 0 赞 0 分享
查看更多