Hatena::ブログ(Diary)

sirocco の書いてもすぐに忘れるメモ

2009-09-04

Gauche で DBI(Postgresql)

| 00:18 |

まず、Postgresqlドライバを入れる。

$ wget http://www.kahua.org/download/dbi/Gauche-dbd-pg-0.2.1.tgz
$ tar zxf Gauche-dbd-pg-0.2.1.tgz
$ cd Gauche-dbd-pg-0.2.1
$ ls
$ lv README
$ which pg_config
/usr/bin/pg_config
$ rpm -qf /usr/bin/pg_config
postgresql-devel-8.1.11-1.el5_1.1
$ ./configure 
$ make
$ sudo make install

Gauche ユーザリファレンス 11.4 dbi - データベース非依存アクセス層 を参考に。

(use dbi)
(use gauche.collection)
 
(define (select-db dbname sql)
  (guard (e ((<dbi-error> e) (raise e)))
    (let* ((conn   (dbi-connect (string-append "dbi:pg:dbname=" dbname)))
           (result (dbi-execute (dbi-prepare conn sql)))
           (getter (relation-accessor result)))
       (let ((dat (map (lambda (row)
                             (map (lambda (x) (getter row x)) 
                                  (relation-column-names result)))
                       result)))
          (dbi-close conn)
          dat))))
 
(print (select-db "dbname" "SELECT code,post FROM jusho"))
$ gosh db.scm
((0 9999999) (1 2720035) (2 2720034) (3 2720033) (4 2720032) (5 2720826) 
 (6 2720031) (7 2720824) (8 2720827) (9 2720834) (10 2720835) (11 2720825)
 (12 2720832) (13 2720833) (14 2720025) (15 2720026) (16 2720015) 
 (17 2720024) (18 2720023))
トラックバック - http://d.hatena.ne.jp/sirocco/20090904/1252077503