ruby-postgresをインストールしてみた(Windows編) | ラブレタ ー それから

ruby-postgresをインストールしてみた(Windows編)


★前提
ruby(InstantRails-1.7)とPostgreSQLはインストール済み
環境はWindowsXP SP3。

LinuxでもだいたいOK。

★環境変数
~~~;C:\InstantRails-1.7\ruby\bin;C:\Program Files\PostgreSQL\8.3\bin

★ruby-postgresのインストール
C:\InstantRails-1.7\ruby\bin>set POSTGRES_INCLUDE=C:\Program Files\PostgreSQL\8.3\include

C:\InstantRails-1.7\ruby\bin>set POSTGRES_LIB=C:\Program Files\PostgreSQL\8.3\lib

C:\InstantRails-1.7\ruby\bin>gem install ruby-postgres

Bulk updating Gem source index for: http://gems.rubyforge.org
Select which gem to install for your platform (i386-mswin32)
1. ruby-postgres 0.7.1.2006.04.06 (ruby)
2. ruby-postgres 0.7.1.2006.04.06 (mswin32)
3. ruby-postgres 0.7.1.2006.04.05 (ruby)
4. ruby-postgres 0.7.1.2006.04.05 (mswin32)
5. Skip this gem
6. Cancel installation
> 2
Successfully installed ruby-postgres-0.7.1.2006.04.06-mswin32
Installing ri documentation for ruby-postgres-0.7.1.2006.04.06-mswin32...
Installing RDoc documentation for ruby-postgres-0.7.1.2006.04.06-mswin32...

★インストールの確認
C:\InstantRails-1.7\ruby\bin>gem list

*** LOCAL GEMS ***

actionmailer (1.3.3)
Service layer for easy email delivery and testing.

(省略)

ruby-postgres (0.7.1.2006.04.06)
Ruby extension library providing an API to PostgreSQL

(省略)

C:\InstantRails-1.7\ruby\bin>

★ディレクトリの確認
C:\InstantRails-1.7\ruby\lib\ruby\gems\1.8\gems 以下に

ruby-postgres-0.7.1.2006.04.06-mswin32 のディレクトリが作成される。

★postgres.soの配置の確認
>ruby -e 'puts $:'
C:/InstantRails-1.7/ruby/lib/ruby/site_ruby/1.8
C:/InstantRails-1.7/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt
C:/InstantRails-1.7/ruby/lib/ruby/site_ruby
C:/InstantRails-1.7/ruby/lib/ruby/1.8
C:/InstantRails-1.7/ruby/lib/ruby/1.8/i386-mswin32
.

>ruby -rpostgres -e ''
ruby: no such file to load -- postgres (LoadError)

★ postgres.so を C:/InstantRails-1.7/ruby/lib/ruby/1.8 にコピー
>ruby -rpostgres -e ''
>

★libeay32.dll、ssleay32.dllをpostgreSQLのものに置き換える
libeay32.dll
ssleay32.dll



C:\Program Files\PostgreSQL\8.3\binより

C:\InstantRails-1.7\ruby\binにコピー
(但し、以前のものは「~.old」のように拡張子などを変えるなどして保存しておく。

★★★★★★
===使い方===
01 : require "postgres"
02 :
03 : io0 = open("test.txt","w")
04 :
05 : conn = PGconn.connect('localhost',5432,'','','(データベース名)','(ユーザ名)','(パスワード)')
06 : conn.set_client_encoding('UTF8')
07 :
08 : res = conn.query("SELECT * FROM songbook ORDER BY song_sort")
09 : # row[0] #SEQ(seq)
10 : # row[1] #曲名sort(song_sort)
11 : # row[2] #演奏者名sort(player_sort)
12 : # row[3] #曲名(song)
13 : # row[4] #演奏者名(player)
14 : # row[5] #日付(blog_date)
15 : # row[6] #URL(url)
16 :
17 : res.each do |row|
18 : io0.puts "by name : song:#{row['song']} / player:#{row['player']}"
19 : io0.puts "by position: song:#{row[3]} / player:#{row[4]}"
20 : io0.puts "\n"
21 : end
22 :
23 : # 終了処理
24 :
25 : res.clear
26 : conn.close


実行結果

by name : song:1234 / player:Feist
by position: song:1234 / player:Feist

by name : song:25 or 6 to 4 (長い夜) / player:Chicago
by position: song:25 or 6 to 4 (長い夜) / player:Chicago




res PGresultクラス / 各レコードの配列
row PGrowクラス / 各フィールドの配列(フィールド名、ポジションで指定可能)
          ハッシュのような感じにもなる?。。。(もっと研究しないと)

★参考

http://archive.netbsd.se/?ml=pgsql-interfaces&a=2005-11&t=1496893