blog/content/post/2012/04/15/2012-04-15-00001552.md

207 lines
9.6 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: Gaucheでsqlite3を使用する
author: kazu634
date: 2012-04-15
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5457;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- gauche
- sqlite3
---
<div class="section">
<p>
いつの間にか「<a href="http://practical-scheme.net/wiliki/wiliki.cgi?kikuchi" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://practical-scheme.net/wiliki/wiliki.cgi?kikuchi', 'kikuchi');" target="_blank">kikuchi</a>」で配布されていたsqlite3用のライブラリがダウンロードできなくなっていて、一瞬パニックになりましたが、githubで別な方が公開していらっしゃるみたいでした。「<a href="https://github.com/mhayashi1120/Gauche-dbd-sqlite3" onclick="__gaTracker('send', 'event', 'outbound-article', 'https://github.com/mhayashi1120/Gauche-dbd-sqlite3', 'mhayashi1120/Gauche-dbd-sqlite3 ? GitHub');" target="_blank">mhayashi1120/Gauche-dbd-sqlite3 ? GitHub</a>」です。
</p>
<h4>
インストール方法
</h4>
<p>
とりあえず以下のようにしてインストールしました:
</p>
<pre class="syntax-highlight">
% git clone git://github.com/mhayashi1120/Gauche-dbd-sqlite3.git
% <span class="synStatement">cd</span> Gauche-dbd-sqlite3/
% ./DIST gen
% ./configure
% make
% make <span class="synStatement">install</span>
</pre>
<p>
makeのところでなんかこけた記憶があるのですが、たしか「-l」オプションをひとつMakefileから削除した記憶が。。。
</p>
<h4>
使い方
</h4>
<p>
とりあえずこんな感じで使ってみました。ちなみにカレントディレクトリにquote.dbというsqlite3用のファイルがある前提です:
</p>
<pre class="syntax-highlight">
<span class="synComment">;; ==============================</span>
<span class="synComment">;; Libraries:</span>
<span class="synComment">;; ==============================</span>
<span class="synSpecial">(</span>use dbi<span class="synSpecial">)</span>
<span class="synSpecial">(</span>use gauche.collection<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">define</span> <span class="synSpecial">(</span>execute-sql dbfile sql<span class="synSpecial">)</span>
<span class="synSpecial">(</span>guard
<span class="synSpecial">(</span>e <span class="synSpecial">((</span><span class="synConstant">&#60;dbi-parameter-error&#62;</span> e<span class="synSpecial">)</span> <span class="synSpecial">(</span>dbi-close connect<span class="synSpecial">))</span>
<span class="synSpecial">((</span><span class="synConstant">&#60;dbi-error&#62;</span> e<span class="synSpecial">)</span> <span class="synSpecial">(</span>exit <span class="synConstant">1</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span><span class="synStatement">else</span> <span class="synSpecial">(</span>raise e<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">let*</span> <span class="synSpecial">((</span>connect <span class="synSpecial">(</span>dbi-connect <span class="synSpecial">(</span><span class="synIdentifier">string-append</span> <span class="synConstant">&#34;dbi:sqlite3:&#34;</span>
dbfile<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>query <span class="synSpecial">(</span>dbi-prepare connect sql<span class="synSpecial">))</span>
<span class="synSpecial">(</span>result <span class="synSpecial">(</span>dbi-execute query<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>dbi-close connect<span class="synSpecial">)</span>
result<span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synStatement">define</span> <span class="synConstant">*sql-result*</span>
<span class="synSpecial">(</span>execute-sql <span class="synConstant">&#34;quote.db&#34;</span>
<span class="synConstant">&#34;SELECT * from quote ORDER BY id DESC LIMIT 10;&#34;</span><span class="synSpecial">))</span>
</pre>
<p>
execute-sqlは
</p>
<ol>
<li>
sqlite3用のファイル
</li>
<li>
実行するsql文
</li>
</ol>
<p>
を引数にとり、実行した結果(リレーション)を返します。
</p>
<h5>
1カラム目と2カラム目を標準出力に表示する
</h5>
<p>
次のようにしてみました:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span><span class="synIdentifier">for-each</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>row<span class="synSpecial">)</span>
<span class="synSpecial">(</span>format <span class="synSpecial">(</span><span class="synIdentifier">current-output-port</span><span class="synSpecial">)</span>
<span class="synConstant">&#34;id:~a\ndate:~a\n&#34;</span>
<span class="synSpecial">(</span>dbi-get-value row <span class="synConstant"></span><span class="synSpecial">)</span>
<span class="synSpecial">(</span>dbi-get-value row <span class="synConstant">1</span><span class="synSpecial">)))</span>
<span class="synConstant">*sql-result*</span><span class="synSpecial">)</span>
</pre>
<p>
実行結果はこのような感じです:
</p>
<pre class="syntax-highlight">
% gosh <span class="synConstant">2012-04-14-122932</span>.scm
id:<span class="synConstant">10370</span>
date:<span class="synConstant">1334359203</span>
id:<span class="synConstant">10369</span>
date:<span class="synConstant">1334361235</span>
id:<span class="synConstant">10368</span>
date:<span class="synConstant">1334327565</span>
id:<span class="synConstant">10367</span>
date:<span class="synConstant">1334239319</span>
id:<span class="synConstant">10366</span>
date:<span class="synConstant">1334235069</span>
id:<span class="synConstant">10365</span>
date:<span class="synConstant">1334186403</span>
id:<span class="synConstant">10364</span>
date:<span class="synConstant">1334177624</span>
id:<span class="synConstant">10363</span>
date:<span class="synConstant">1334177843</span>
id:<span class="synConstant">10362</span>
date:<span class="synConstant">1334152357</span>
id:<span class="synConstant">10361</span>
date:<span class="synConstant">1334152804</span>
</pre>
<h5>
DBのカラム名を取得し、標準出力に表示する
</h5>
<p>
次のようにしてみました:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span><span class="synIdentifier">for-each</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>column-name<span class="synSpecial">)</span>
<span class="synSpecial">(</span>format <span class="synSpecial">(</span><span class="synIdentifier">current-output-port</span><span class="synSpecial">)</span>
<span class="synConstant">&#34;~a\n&#34;</span>
column-name<span class="synSpecial">))</span>
<span class="synSpecial">(</span>relation-column-names <span class="synConstant">*sql-result*</span><span class="synSpecial">))</span>
</pre>
<p>
実行結果はこのような感じです:
</p>
<pre class="syntax-highlight">
% gosh <span class="synConstant">2012-04-14-122932</span>.scm
id
date
quote
<span class="synStatement">source</span>
md5
</pre>
<h5>
各カラムの値を取得し、標準出力に表示する
</h5>
<p>
次のようにしてみました:
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span><span class="synStatement">let*</span> <span class="synSpecial">((</span>column-names
<span class="synSpecial">(</span>relation-column-names <span class="synConstant">*sql-result*</span><span class="synSpecial">))</span>
<span class="synSpecial">(</span>getter <span class="synSpecial">(</span>relation-accessor <span class="synConstant">*sql-result*</span><span class="synSpecial">)))</span>
<span class="synSpecial">(</span><span class="synIdentifier">for-each</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>row<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synIdentifier">map</span> <span class="synSpecial">(</span><span class="synStatement">lambda</span> <span class="synSpecial">(</span>column<span class="synSpecial">)</span>
<span class="synSpecial">(</span>format <span class="synSpecial">(</span><span class="synIdentifier">current-output-port</span><span class="synSpecial">)</span>
<span class="synConstant">&#34;~a:\n~a\n&#34;</span>
column
<span class="synSpecial">(</span>getter row column<span class="synSpecial">)))</span>
column-names<span class="synSpecial">))</span>
<span class="synConstant">*sql-result*</span><span class="synSpecial">))</span>
</pre>
<p>
実行結果はこのような感じです:
</p>
<pre class="syntax-highlight">
% gosh <span class="synConstant">2012-04-14-122932</span>.scm
id:
<span class="synConstant">10370</span>
date:
<span class="synConstant">1334359203</span>
quote:
抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 <span class="synStatement">(</span>via @medtoolz<span class="synStatement">)</span>
<span class="synStatement">source</span>:
http://twitter.com/medtoolz/<span class="synStatement">status</span>/<span class="synConstant">190930863851184129</span>
md5:
6392e235cebdae0ac9c87ee958648fc0
</pre>
<h4>
さてと
</h4>
<p>
もう少し遊んでみます。
</p>
</div>