2012年11月7日 星期三



perl how to connect mssql with DBI DBD::ODBC FreeTDS unixODBC


安裝YAML
#cpan>install YAML
安裝DBI
#cpan>install DBI
安裝unixODBC(Free ODBC Driver Manager, Drivers & Tools.)
#sudo apt-get install unixODBC unixODBC-dev
安裝DBD::ODBC
#cpan>install DBD::ODBC
安裝DataBase Driver FreeTDS
#sudo apt-get install freetds-bin freetds-dev tdsodbc
檢查 tsql Compile-time settings
#tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.82
             freetds.conf directory: /etc/freetds
     MS db-lib source compatibility: no
        Sybase binary compatibility: yes
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 4.2
                              iODBC: no
                           unixodbc: yes
freetds.conf上設定DSN
#vim /etc/freetds/freetds.conf
//在設定檔最下方加上兩條DSN設定,如此使用tsql就可以用DSN來連線「tsql -S ERP -U sa -P password」
[ERP]
        host = 192.168.128.253
        port = 1433
        tds version = 8.0

[SQL2008X64]
        host = 192.168.128.219
        port = 1433
        tds version = 8.0
測試MSsql連線
#sudo tsql -S 192.168.128.253 -U sa -P password
1>
如果出現上面的符號就表示已經可以連上MSsql DataBase了
查詢資料時中文無法正常顯示的設定方法
#sudo vim /etc/freetds/freetds.conf
[global]
  # TDS protocol version
  tds version = 8.0
  client charset = UTF-8
正確設定好 TDS version似乎就能正常顯示中文了,client charset有沒有設定好像沒有影響。
如何設定正確的 tds version 可以參考 「History of TDS Version」。
也可以在命令列模式下直接指定 TDSVER=X.X 的版本為何。例如:
1TDSVER=7.0 tsql -H 192.168.1.100 -p 1433 -U sa
unixODBC設定/etc/odbcinst.ini、/etc/odbc.ini
建立tds.driver.template、tds.datasource.template模板
#cd ~
#vim tds.driver.template
[FreeTDS]
Description     = FreeTDS driver
Driver          = /usr/lib/odbc/libtdsodbc.so
Setup           = /usr/lib/odbc/libtdsS.so
tds version     = 8.0
#vim tds.datasource.template
[ERP]
Description         = ERP Connection
Driver              = FreeTDS
Trace               = Yes
TraceFile           = /tmp/odbc.log
Database            = A01B
Servername          = ERP

[SQL2008X64]
TDS_Version         = 8.0
Description         = SQL2008X64 Connection
Driver              = FreeTDS
Trace               = Yes
TraceFile           = /tmp/odbc.log
Server              = 192.168.128.219
Port                = 1433
Socket              =
#sudo odbcinst -i -d -f tds.driver.template
將freetds driver加入unixODBC管理,可以在/etc/odbcinst.ini看到剛在tds.driver.template上的設定。
#sudo odbcinst -i -s -l -f tds.datasource.template
將寫在tds.datasource.template上的DSN,記錄於/etc/odbc.ini上。
模板格式設定,可由man odbcinst查詢得知(說明的最下面部份)。
注意事項
unixODBC 通過freeTDS 訪問MS SQL Server 有兩種配置方式
其一是將服務器信息寫在freeTDS的配置文件/etc/freetds/freetds.conf  中, /etc/odbc.ini 中使用Servername 來指向freetds.conf中設定的DSN。如上例中的 [ERP]
另一種方式是將服務器信息也一併寫在/etc/odbc.ini 中。如上例中的[SQL2008X64]
注意,關鍵字有所不同。 例如, freetds.conf 中的tds version 在/etc/odbc.ini 中為TDS_Version。
方式(2)相對簡單,但只有少數幾個關鍵字可以控制freetds,至於freetds的其它特徵則使用freetds的預設值。
方式(1)雖然複雜一些,但對freetds可進行更細緻的控制,例如可指定客戶端的字符集。
在透過isql DSN UID PASSWD 模擬perl運行的結果,不意外的話中文顯示應該是正常的。
perl 執行MSsql資料庫查詢
#vim test_connect.pl
use strict;
use DBI;
my $dbh = DBI->connect("dbi:ODBC:ERP","sa","password")
                 or die "$DBI::errstr\n";
my $statement =<<end;
        select MV001,NAME,MV002,MV003,MV098,NEXTTIME_CHANGEPASSWD from YCMSMV
        where NEXTTIME_CHANGEPASSWD < '20110725' and MV001 not like '9%';
end

my $emps = $dbh->selectall_arrayref($statement,{ Slice => {} });
foreach my $emp ( @$emps ) {
    print "Employee: $emp->{NAME}\n";
}

my $rc = $dbh->disconnect  or warn $dbh->errstr;
#perl test_connect.pl
Employee: 陳X仁
Employee: 陳X旭
Employee: 方X治
Employee: 大X徹也
Employee: 澤X正志
Employee: 黃X菁
Employee: 小X康雄
Employee: 鄭X村
Employee: 林X龍

沒有留言:

張貼留言