一、安裝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 Makefile.PL
make
make test
make install
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;