一、安装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;