通过freetds用perl访问sybase和mssql

一、安装freetds

下载并解压freetds-patched.tar.gz

$ tar zxvf freetds-patched.tar.gz

./configure --prefix=/opt/local/freetds --enable-msdblib --enable-sybase-compat --with-gnu-ld --enable-shared --enable-static --with-unixodbc=/usr --with-tdsver=7.1

make&sudo make install

设置好环境变量

二、修改/opt/local/freetds/etc/freetds.conf

[global]
        # TDS protocol version
;	tds version = 4.2

	# Whether to write a TDSDUMP file for diagnostic purposes
	# (setting this to /tmp is insecure on a multi-user system)
;	dump file = /tmp/freetds.log
;	debug flags = 0xffff

	# Command and connection timeouts
;	timeout = 10
;	connect timeout = 10
	
	# If you get out-of-memory errors, it may mean that your client
	# is trying to allocate a huge buffer for a TEXT field.  
	# Try setting 'text size' to a more reasonable limit 
	text size = 64512

	client charset = UTF-8

# A typical Sybase server
[egServer50]
	host = localhost
	port = 5000
	tds version = 5.0

# A typical Microsoft server
[egServer70]
	host = 192.168.0.177
	port = 1433
	tds version = 7.0

经实践这里

tds version = 5.0

貌似无效(这会影响后面perl的代码)

测试

l$ tsql -SegServer70 -Umymotif -Pwxwpxh

locale is "zh_CN.UTF-8"

locale charset is "UTF-8"

using default charset "UTF-8"

1> 

mssql通过

$ tsql  -S egServer50 -U mymotif -Pwxwpxh

locale is "zh_CN.UTF-8"

locale charset is "UTF-8"

using default charset "UTF-8"

Error 20002 (severity 9):

Adaptive Server connection failed

There was a problem connecting to the server

sybase出错

$ TDSVER=5.0 tsql -SegServer50 -Umymotif -Pwxwpxh

locale is "zh_CN.UTF-8"

locale charset is "UTF-8"

using default charset "UTF-8"

1> 

二、安装DBD-Sybase

wget http://www.peppler.org/downloads/DBD-Sybase-1.15.tar.gz

获取源代码

export SYBASE=/opt/local/freetds (注意不是SYBASE=/opt/sybase)

接着就是安装perl模块的标准动作

perl Makefile.PL 

make 

make test 

make install


四、perl测试代码

1、mssqltest.pl

#!/usr/bin/perl

use DBI;
use DBD::Sybase; 

$dbname="mymotif";
$user="mymotif";
$passwd="wxwpxh";

#SQLSERVER 字串对应于 /opt/local/freetds/etc/freetds.conf 里面的 [SQLSERVER] 
$dsn = "DBI:Sybase:server=egServer70;database=$dbname";
 

$dbh = DBI->connect($dsn,$user,$passwd) or die "can't connect to database : $DBI::errstr";

$sth=$dbh->prepare("select * from STUDENT");
$sth->execute;
while (@recs=$sth->fetchrow_array) {
print $recs[0].":".$recs[1].":".$recs[2]."\n";
}
$dbh->disconnect;

2、sybtest.pl

#!/usr/bin/perl

use DBI;
use DBD::Sybase; 

$dbname="testdb";
$user="mymotif";
$passwd="wxwpxh";


#egServer50 字串对应于 /opt/local/freetds/etc/freetds.conf 里面的 [egServer50] 
$ENV{TDSVER} = "5.0";
$dsn = "DBI:Sybase:server=egServer50;database=$dbname";


$dbh = DBI->connect($dsn,$user,$passwd) or die "can't connect to database : $DBI::errstr";

$sth=$dbh->prepare("select * from STUDENT");
$sth->execute;
while (@recs=$sth->fetchrow_array) {
print $recs[0].":".$recs[1].":".$recs[2]."\n";
}
$dbh->disconnect;


转载于:https://my.oschina.net/u/2245781/blog/626203

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值