標籤

4GL (1) 人才發展 (10) 人物 (3) 太陽能 (4) 心理 (3) 心靈 (10) 文學 (31) 生活常識 (14) 光學 (1) 名句 (10) 即時通訊軟體 (2) 奇狐 (2) 音樂 (2) 產業 (5) 郭語錄 (3) 無聊 (3) 統計 (4) 新聞 (1) 經濟學 (1) 經營管理 (42) 解析度 (1) 遊戲 (5) 電學 (1) 網管 (10) 廣告 (1) 數學 (1) 機率 (1) 雜趣 (1) 證券 (4) 證券期貨 (1) ABAP (15) AD (1) agentflow (4) AJAX (1) Android (1) AnyChart (1) Apache (14) BASIS (4) BDL (1) C# (1) Church (1) CIE (1) CO (38) Converter (1) cron (1) CSS (23) DMS (1) DVD (1) Eclipse (1) English (1) excel (5) Exchange (4) Failover (1) FI (57) File Transfer (1) Firefox (2) FM (2) fourjs (1) gladiatus (1) google (1) Google Maps API (2) grep (1) Grub (1) HR (2) html (23) HTS (8) IE (1) IE 8 (1) IIS (1) IMAP (3) Internet Explorer (1) java (3) JavaScript (22) jQuery (6) JSON (1) K3b (1) LED (3) Linux (112) Linux Mint (4) Load Balance (1) Microsoft (2) MIS (2) MM (51) MSSQL (1) MySQL (27) Network (1) NFS (1) Office (1) Oracle (125) Outlook (3) PDF (6) Perl (59) PHP (33) PL/SQL (1) PL/SQL Developer (1) PM (3) Postfix (2) postfwd (1) PostgreSQL (1) PP (50) python (1) QM (1) Red Hat (4) Reporting Service (28) ruby (11) SAP (234) scp (1) SD (16) sed (1) Selenium-WebDriver (5) shell (5) SQL (4) SQL server (8) SQuirreL SQL Client (1) SSH (2) SWOT (3) Symantec (2) T-SQL (7) Tera Term (2) tip (1) tiptop (22) Tomcat (6) Trouble Shooting (1) Tuning (5) Ubuntu (33) ufw (1) utf-8 (1) VIM (11) Virtual Machine (2) vnc (3) Web Service (2) wget (1) Windows (19) Windows (1) WM (6) youtube (1) yum (2)

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龍

沒有留言:

張貼留言