本文将提供一些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();
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 炉石传说移动端商店打不开怎么办 移动端商店打不开解决方法分享
- 王菲《我的世界2》日本唱片志限量编号10CD[WAV+CUE]
- 乌兰托娅《爱从草原来HQII》头版限量编号[低速原抓WAV+CUE]
- 魏妙如.2017-《伟大的旅行》[WAV分轨]
- 科乐美送短跑冠军牌佬豪华礼包:顶罕卡片、决斗盘等
- 重大进展!PS4模拟器运行《重力眩晕:重制版》演示
- 南半球爱好者的福利 日本御姐级樱花妹高挑性感
- 玩家热议:哪些游戏通关后才算正式开始?
- 知名博主称赞《影之刃零》:有望成为现代最强之一?
- 泳衣快兜不住了!《莱莎工房》莱莎琳新手版开放预订
- 5.1版本「绘想游迹」全新游迹特效展示
- 原神纳塔篇OST&四周年分享H5正式上线
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】