骨のダイスを転がそう

2009|01|02|03|04|05|06|07|10|12|
2010|01|02|03|04|05|06|07|08|09|10|11|
2011|02|03|05|06|07|08|09|10|11|12|
2012|01|02|04|05|09|10|11|12|
2013|01|02|04|
2014|11|
2015|01|04|05|
2016|06|07|08|09|11|
2017|01|02|03|04|
2018|03|

2011-12-29

_ [Ruby] Debian Squeeze から ruby/DBI で SQL Server 2008 に接続

いろいろと依存パッケージが多いので、順番にやっていかないと混乱する。ファイアウォールとかの問題はクリアしていることが前提。

SQL Server に接続するまで

FreeTDS のインストール

aptitude install freetds-bin freetds-common

/etc/freetds/freetds.conf の設定

#   $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".

# Global settings are overridden by those in a database
# server specific section
[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
# 追加
[HogeServer]
        host           = hogehoge.sample.org
        port           = 1433
        tds version    = 8.0
        charset        = utf8
        client charset = utf8
user01@fuga:~$ tsql -S HogeServer -D sampledb -U sqluser -P xxxxx

を実行して

locale is "ja_JP.UTF-8"
locale charset is "UTF-8"
Default database being set to sampledb
1>

プロンプトが出たら接続成功。

odbc のデータソース設定

unixodbc のインストール

aptitude install unixodbc unixodbc-dev

tdsodbc のインストール

aptitude install tdsodbc

/etc/odbcinst.ini (/usr/share/tdsodbc/odbcinst.ini からコピーする)

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver      = /usr/lib/odbc/libtdsodbc.so
Setup       = /usr/lib/odbc/libtdsS.so
CPTimeout   =
CPReuse     =

/etc/odbc.ini

[ODBC Data Sources]
SampleDSN = sampledb Data Source Name

[SampleDSN]
Driver      = FreeTDS
Description = Microsoft SQL Server
Servername  = HogeServer
Database    = sampledb

isqlコマンドで接続を確認

isql -v SampleDSN sqluser xxxxx
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

プロンプトが出れば、ODBC のデータソースネーム設定も成功。SQL が実行できるはず。

ここまでは ruby 関係なし。

Ruby/DBI の設定

必要な gem をインストール

gem install ruby-odbc
gem install dbi
gem install dbd-odbc

テストスクリプトを実行。

require 'rubygems'
require 'dbi'

SQL ="SELECT name FROM sysobjects WHERE xtype = 'U';"

DBI.connect('dbi:ODBC:sampleDSN', 'sqluser', 'xxxxx') do | dbh |
  dbh.select_all(SQL) do | row |
    p row
  end
end

結果が出力されれば成功。作者、パッケージャのみなさんに感謝。