1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows環境のRubyで、ODBCドライバを使ってFileMakerにSQL接続する (ruby-odbc)

Last updated at Posted at 2022-04-06

Pyhonであればpyodbcで一発なのですが、それをWindows環境のrubyで出来るようにするためのメモ。

FileMaker側の準備をする

公式ドキュメントがあるので略。

なおFileMaker Proでは、以下のような制限がありますので注意。

共有 FileMaker Pro データベースへのアクセス
FileMaker Pro -- 最大 5 の接続、およびローカルアクセス( 同じコンピュータ ) のみサポート。
https://help.claris.com/ja/odbc-jdbc-guide.pdf

ドライバの接続テスト

Clipboard_03-31-2024_01.png

Rubyインタプリタが64bitなら、ODBCデータソースも64bitですよ!

まずはpowershell から接続できるか検証

スクリプトを書く

test.ps1
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$WarningPreference = "Continue"
$VerbosePreference = "Continue"
$DebugPreference = "Continue"

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Data")

$connectionString = "DSN=FileMaker ODBC;uid=user;pwd=user;"

$odbcCon = New-Object System.Data.Odbc.OdbcConnection($connectionString)
$odbcCon.open()

$odbcCmd = New-Object System.Data.Odbc.OdbcCommand
$odbcCmd.Connection = $odbcCon

$odbcCmd.CommandText = "SELECT count(*) FROM item"
$odbcReader = $odbcCmd.ExecuteReader()
while($odbcReader.Read()) {
  $odbcReader[0].ToString()
}
$odbcReader.Dispose()
$odbcCmd.Dispose()

$odbcCon.Close()
$odbcCon.Dispose()

実行してみる

C:\ powershell -ExecutionPolicy Bypass
PS C:\ .\odbctest.ps1
195

Ruby経由で接続してみる

RubyInsteller でビルドしてある、以下のバージョンで検証しました。

ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x64-mingw32]

devkitが入っていないのであれば、Msys2を入れる

C:\>winget search msys2
名前            ID                               バージョン ソース
-------------------------------------------------------------------
MSYS2           msys2.msys2                      20220319   winget

C:\>winget install msys2.msys2

ruby-odbcを入れる

C:\gem install ruby-odbc

rubyからの接続方法

マニュアル通りの以下の方法では、windows環境ではエラーになってしまう・・。

require "odbc_utf8" 

con = ODBC.connect("DRIVER={FileMaker ODBC};SERVER=localhost;DATABASE=altezza", "user", "user")
testodbc.rb:8:in `initialize': HY090 (0) [Microsoft][ODBC Driver Manager] 文字列またはバッファーの長さが無効です。 (ODBC::Error)

以下のようなやり方で、手動でDSNを作ってあげる必要がある。

require "odbc_utf8"

driver = ODBC::Driver.new
driver.name = 'odbc'
driver.attrs = {}
driver.attrs["DRIVER"] = "FileMaker ODBC"
driver.attrs["HOST"] = "localhost"
driver.attrs["DATABASE"] = "altezza"
driver.attrs["UID"] = "user"
driver.attrs["PWD"] = "user"

conn = ODBC::Database.new.drvconnect(driver)
res = conn.run("SELECT count(*) FROM item")
res.each{ |rec|
	p rec
}

できました!

どなたか、ruby-odbc ドライバのメンテナにフィードバックしてくれないですかね・・。

ところで、ActiveRecordはFileMakerDBに対応しているの?

activerecord-odbc-adapter というgemがありますが、FileMaker対応はNoです。Oracleとかの対応はされているのですが。全般に渡って手を入れないと機能しませんわ。

さらに問題

varcharについてSELECTすると、512バイトよりも長い部分は文字化けします。。困りました。直す方法は分かりません。

一応報告したのですが、なしのつぶて。。

参考文献

外部リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?