平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“Windows和Linux系统下perl连接SQL Server数据库的……”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
从实现思路看,本文将提供一些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_numbern";
}
#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_numbern";
}
#Close the connection
$sth->finish();
$dbh->disconnect();
Linux平台
从实现思路看,若是在Linux平台下运行perl脚本,连接SQL Server数据库需采用到DBD::Sybase包。
安装SQL Server兼容库
Sybase DBD包依赖FreeTDS驱动程序。
FreeTDS下载地址:www.freetds.org
安装FreeTDS驱动的说明文档参见:(链接已移除)
该驱动没有采用到ODBC.
设置数据源
从实现思路看,修改freetds.conf文件包括SQL Server数据库信息,如下所示所示:
[SS_MY_DB]
host = 10.0.0.1 # or host name port = 1433
tds version = 7.0
安装Sybase DBD模块
该模块文档参见:(链接已移除)
在这个场景下,此外,需将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, $phonen";
}
#Close the connection
$sth->finish();
undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase
$dbh->disconnect();
- 让apache2以cgi方式运行perl cgi程序的实现方法
- windows下Apache+MySql+PHP3+PHP4+PERL安装设置
- Win2000+Apache+MySql+PHP4+PERL安装采用小结
- Windows Server 2016 上设置 APACHE+SSL+PHP+perl的教程详解