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

   2015-07-03 0
核心提示:这篇文章主要介绍了Windows和Linux系统下perl连接SQL Server数据库的方法,本文详细的讲解了Windows和Linux系统中perl如何连接Microsoft 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_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();

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Windows API Reference for C#, VB.NET
    不错的.net 下用API的参考站点地址在:http://www.webtropy.com/articles/Win32-API-DllImport-art9.asp 下面摘抄分类,便于大家直接就拿来用: File, Memory, Process, Threading, Time, Console, and Comm control(kernel32.dll) _hread_hwrite_lclose_lcr
    03-16
  • windows下编译lua源码"><转>windows下编译lua源
    因为之前一直使用 lua for windows 来搭建lua的使用环境,但是最新的 lua for windows 还没有lua5.2,我又想用这个版本的lua,所以被逼无奈只能自己编一下lua源码。首先从 lua的官网 下载你想要使用的lua源码,比如我下载的就是lua5.2。解压后内容如下:
    03-16
  • [脚本_Ruby]Windows安装配置Ruby On Rails
    感觉Java学的差不多了,想接触下Ruby On Rails,看看它比Java WEB高效到哪里了,在Ubuntu12.10上弄了两天总是报错提示没有指向的文件sqlite3,不管怎么安装sqlite3都不行,到最后没办法只有跑到WIN7平台下试试,以下就是我搭建Ruby On Rails的过程:    
    02-10
  • Docker windows下安装并搭建Nodejs的webapp
    Docker windows下安装并搭建Nodejs的webapp
    一、关于Docker什么是Docker?Docker 采用go语言编写,是一个开源的应用容器引擎。让开发者可以快速打包他们的应用以及依赖包到一个封装的可移植的容器Image中,然后发布到任何流行的机器( Linux ,windows,Mac等)上,也可以实现虚拟化。容器是使用完全
    02-09
  • WASM 之 Rust 开发(Windows Docker环境)
    WASM 之 Rust 开发(Windows Docker环境)
    环境活动: 学 Rust,免费拿树莓派+Rust 官方文档Windows10 ProDocker (开启 WSL2 Feature 及 Hyper-V 虚拟层)基于Rust的wasm项目开发模板 https://github.com/second-state/ssvm-nodejs-starter/VSCode 及其 Rust 插件另外可以考虑加上 Remote-Docker 插件测
    02-09
  • 我持续推动Rust语言支持Windows XP系统
    前言Rust好像长期以来不支持Windows XP系统。有不少用户发帖提议官方支持XP,基本上都被Rust官方开发人员明白的拒绝了。他们的对话大致上是以这种形式開始和结束的(当中乙方代表官方及其拥趸):甲方:Rust应该支持XP呀乙方:微软官方已经放弃维护XP。我们为
    02-09
  • windows下安装rust windows下安装openwrt
    windows下安装rust windows下安装openwrt
    前言从1.14.0之后的某个版本开始,rust的安装从多个不同平台的安装包合并为一个名为rustup-init.exe了。其中命名中的rustup也是rust的工具链管理器。同时安装也变成命令交互式,非常简洁。本文描述的是windows下的rust安装。下载安装包从官网主页下载安装包ru
    02-09
  • Windows 上安装 Rust 出错及其解决办法
    Windows 上安装 Rust 出错及其解决办法
    首先去 https://www.rust-lang.org/tools/install Rust 官网上下载相应的安装包。直接运行 rustup-init.exe,这一步 会把 rustc, cargo 等组件给安装好。测试一下, rustc --version, cargo --version当使用 cargo build 时 遇到 note: LINK : fatal error
    02-09
  • Rust交叉编译Mac编译Linux/Windows平台
    关于交叉编译一般编程阶段用的是Windows或者Mac系统,部署平台是Linux,这种情况下就需要使用Cross-Compiler交叉编译,意思是可以在当前平台Host下编译出目标平台target的可执行文件,尤其是做ARM平台开发的同学对这个更为熟悉。Rust交叉编译在Github上有一个
    02-09
  • Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)
    Delphi 缩放图像代码 - 支持PNG透明通道(利用
    要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上实际上是利用了Windows的windowscodecs.dll这个文件的功能在VCL里已经封装为TWICImage类 procedure ScalerImage(AImage: TWICImage; AWidth, AHeight: Word);varnWIF: IWICImagingFactory;nWIS: IWICBitm
    02-09
点击排行