CentOSä¸ã§Perlã®DBIã使ã£ã¦SQL Serverã«æ¥ç¶ããæé
Perl
›
ãã¼ã¿ãã¼ã¹
›
here
ãCentOSä¸ã§Perlã®DBIã使ã£ã¦SQL Serverã«æ¥ç¶ããæé ã§ããFreeTDS, UnixODBCã¨ããUnix(Linux)ã®ã©ã¤ãã©ãªã¨Perlã®DBD::ODBCã¢ã¸ã¥ã¼ã«ãé©åã«ã¤ã³ã¹ãã¼ã«ãè¨å®ããå¿ è¦ãããã¾ãã
(試ããç°å¢ã¯CentOS release 5.5 (Final)ããã®è¨äºã¯Red Hat Enterprise Linux, Fedoraã§ãæå¹ã§ããããããã¾ãããã¾ãä»ã®Linuxãã£ã¹ããªãã¥ã¼ã·ã§ã³ã§ãæå¹ã§ããããããã¾ãããSELinuxãç¡å¹åãã¦ãããªãã¨ãã¤ã³ã¹ãã¼ã«ããã¾ããããªãããããã¾ãããã½ã¼ã¹ããã¤ã³ã¹ãã¼ã«ããã®ã§gccãªã©ã®éçºãã¼ã«ã¯äºåã«ã¤ã³ã¹ãã¼ã«ããã¦ãããã¨ãæ³å®ãã¦ãã¾ãã)
ãæ¥ç¶ã®ã¤ã¡ã¼ã¸ãæ¸ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
DBI - ODBC(éä¿¡ã«FreeTDSãããã³ã«ã使ç¨) - SQL Server
ãDBIã§ã¯ãã©ã¤ãã¨ãã¦ODBCãå¿ è¦ã¨ãªãã¾ããããã«å¯¾å¿ããPerlã®ã¢ã¸ã¥ã¼ã«ãDBD::ODBCã§ããã¾ãDBD::ODBCã¯ãUnixODBCã¨ããã©ã¤ãã©ãªãå¿ è¦ã¨ãã¦ãã¾ããUnixODBCã¨ããã©ã¤ãã©ãªã¯SQLãµã¼ãã¼ã¨éä¿¡ããããã«FreeTDSã¨ããã©ã¤ãã©ãªãå¿ è¦ã¨ãã¾ãã
UnixODBCã®ã¤ã³ã¹ãã¼ã«
ãUnixODBCã¨ã¯ODBCã®Unixä¸ã§ã®å®è£ ã®ã²ã¨ã¤ã§ããUnixODBCãã½ã¼ã¹ã³ã¼ãããã¤ã³ã¹ãã¼ã«ãã¾ããããä¾åã¢ã¸ã¥ã¼ã«ãå¢ããã®ã§ããªãã·ã§ã³ã§GUIã®ãµãã¼ãã¯ãªãã«ãã¦ãã¾ããmakeã¯æéããããã®ã§ãã°ããå¾ ã¡ã¾ãããã
cd /usr/local/src wget http://www.unixodbc.org/unixODBC-2.3.0.tar.gz tar -xzvf unixODBC-2.3.0.tar.gz cd unixODBC-2.3.0 ./configure --enable-gui=no make make install
ããã®ã©ã¤ãã©ãªã¯ã/usr/local/libã以ä¸ã«ã¤ã³ã¹ãã¼ã«ããã¾ãã確èªãã¦ã¿ã¾ãããã
ls /usr/local/lib/libodbc.so
FreeTDSã®ã¤ã³ã¹ãã¼ã«
ãFreeTDSã¨ã¯ãTabular Data Streamãã¨ã´ãéä¿¡ãããã³ã«ãå®è£ ããã©ã¤ãã©ãªã§ããUnixODBCãå©ç¨ãã¾ãã
cd /usr/local/src wget http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-0.82.tar.gz tar -xzvf freetds-0.82.tar.gz cd freetds-0.82 export LD_LIBRARY_PATH=/usr/local/lib export LD_RUN_PATH=/usr/local/lib ./configure --with-unixodbc=/usr/local --with-msdblib make make install
ããã®ã©ã¤ãã©ãªã¯ã/usr/local/libã以ä¸ã«ã¤ã³ã¹ãã¼ã«ããã¾ãã確èªãã¦ã¿ã¾ãããã
ls /usr/local/lib/libtdsodbc.so
DBD::ODBCã¢ã¸ã¥ã¼ã«ã®ã¤ã³ã¹ãã¼ã«
ãDBD::ODBCã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ãã¾ãã
export ODBCHOME=/usr/local cpan DBD::ODBC
ãDBD::ODBCã¯ã«ã¼ã権éã§ã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ããã®ã§ãcpanmãªã©ã使ã£ã¦å人ãã£ã¬ã¯ããªã«ã¤ã³ã¹ãã¼ã«ããã®ãããã§ãããã
FreeTDSã®è¨å®
ãFreeTDSã®è¨å®ãã¡ã¤ã«ãç·¨éãã¾ããFreeTDSã®è¨å®ãã¡ã¤ã«ã§æ¥ç¶å ã®æ å ±ãªã©ãè¨è¿°ãã¾ãã
vi /usr/local/etc/freetds.conf
[tdssqlserver] host = 192.168.1.19 port = 1433 tds version = 8.0
ãæ¥ç¶å ã®æ å ±ã¨TDSã®ãã¼ã¸ã§ã³ãæå®ãã¾ããç¾å¨ã¯8.0ã§ããããã§ãã[]ã®ä¸ã®ååã¯UnixODBCã§å©ç¨ãããååã«ãªãã¾ãã
UnixODBCã®è¨å®
ãUnixODBCã®è¨å®ãã¡ã¤ã«ãç·¨éãã¾ãã
vi /usr/local/etc/odbc.ini
[ODBC Data Sources] freetds = FreeTDS ODBC Driver [sqlserver] Driver = /usr/local/lib/libtdsodbc.so Description = Microsoft SQL Server Servername = tdssqlserver Database = dbname
ãDriverã«ã¯FreeTDSã®ã©ã¤ãã©ãªã®ãã¹ãServernameã«ã¯FreeTDSã®è¨å®ãã¡ã¤ã«ã§è¨å®ããååãDesDatabaseã«ã¯æ¥ç¶å ã®ãã¼ã¿ãã¼ã¹åãæå®ãã¾ãã
FreeTDSã¨UnixODBCã®è¨å®ã®ç¢ºèª
ãã¾ãFreeTDSã®è¨å®ãæ£ãããã©ãããtsqlã¨ããã³ãã³ããèµ·åãããã¨ã§ç¢ºèªãã¦ã¿ã¾ãããã
ã次ã«è¨å®ãæ£ãããã©ãããtsqlã¨isqlã使ç¨ãã¦è©¦ãã¦ã¿ã¾ãããã
tsql -S tdssqlserver -U kimoto -P pipipi
ãtsqlã§ã®æ¥ç¶ãæåãããFreeTDSã®è¨å®ãæ£ãããã¨ã確èªã§ãã¾ããexitã§çµäºã§ãã¾ãã
isql sqlserver kimoto pipipi
ãisqlã§ã®æ¥ç¶ãæåãããUnixODBCã®è¨å®ãæ£ãããã¨ã確èªã§ãã¾ããEnterãã¼ã§çµäºã§ãã¾ãã
SQL Serverã«Perlããæ¥ç¶ãã
ã次ã«DBIã¢ã¸ã¥ã¼ã«ãã使ç¨ãã¦ã¹ã¯ãªããããæ¥ç¶ã§ããã°æåã§ãã
use strict; use warnings; use DBI; my $dbi = DBI->connect('dbi:ODBC:sqlserver', 'kimoto', 'pipipi') or die $!;