電子書籍の厳選無料作品が豊富!

sqlコマンドで.mode htmlを使ってからselectすると
テーブルで返ってくるのでそのままrubyでも出力できるとコードが短くなって楽だと思ったんですが、うまくできません。
どうかやり方をお教えください。

rubyを始めたばかりでexecuteの実行結果がどのような構造になっているかもよくわかりません。
こちらの方もお教えいただけると幸いです。

下記のような使い方をしたいです。

<%# -*- coding:shift_jis -*- %>

<% require 'rubygems' # RubyGems のインストール %>
<% require 'dbi' #DBIの利用 %>

<% dbh=DBI.connect('DBI:SQLite3:fruits.db') %>

<% #HTML形式で受け取る %>
<% #dbh.do(".mode html;") %>

<%# execute select statement%>
<% sth=dbh.execute("select * from fruits;") %>

<html>
<head>
<meta http-equiv="Context-Type" content="text/html;charset Shift_JIS"/>
<title>フルーツ管理</title>
</head>
<body>
<% #テーブルで出力 %>
<table>
<%#=sth %>
</table>
</body>
</html>

<%# relese results %>
<% sth.finish %>

<%# disconnect database %>
<% dbh.disconnect %>

A 回答 (2件)

> この@がついたのを何というんでしょうか?


……うん、やっぱりあなたは DBI やら HTML 出力やらを取り扱う前に Ruby の基礎を勉強したほうがいいです。
これについて答えるのは簡単ですが、さすがにこれを知らないというのはTシャツにサンダル履きで富士登山をしますと言うのと同じです。そんな人を見かけたならばあなたももう少し登山向けの装備をしろと言うでしょう、それと同じであなたがやろうとしていることに対して持っている知識が明らかに足りていないからのアドバイスです。
    • good
    • 0

SQLite3 のコマンドシェルでの入力のうち . で始まるものは SQL ではなく SQLite3 コマンドシェル自体へのコマンドです。

ですのでコマンドシェルを経由しない DBI では使えません。
http://www.sqlite.org/cli.html
……というか、そんな楽をしようとしたらあかんよw

それと、DB 検索結果の HTML 出力をしようとしているようですが、
> rubyを始めたばかりでexecuteの実行結果がどのような構造になっているかもよくわかりません。
と言っている状態ではそれを HTML として出力するのはまだ先の話です。
好奇心と目標があるのはいいのですが、そこに至る過程を抜かしていきなり目標に降り立とうとしても、基礎がしっかりしていない以上その目標地点にあるものを理解することは困難です。回り道のように思われるかもしれませんが、基礎をしっかり勉強される方が結局は早く目標に到達できると思います。

とりあえず、SELECT についての「executeの実行結果」はフェッチするものですよ。

あ、そうそう。
DBI は 1.8 までしかサポートしていないし、メンテナンスも行われていないようなので代わりに RDBI を使った方がいいみたいですよ。
http://qiita.com/weal/items/2c28f12a23c501db37f2

この回答への補足

.は使えないんですね、質問してよかったです。
最後に2つだけ。sth.inspectで下記のようなものが出力されたのですが、この@がついたのを何というんでしょうか?もうひとつは、なぜshtに格納されていないのにsth.each do |row|などでデータを取得できるんですか?

#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @stmt=#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @remainder="", @columns=["id", "name", "description", "price"], @types=["int", "varchar(100)", "text", "int"], @results=#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @stmt=#>>, @result=#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @stmt=#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @remainder="", @columns=["id", "name", "description", "price"], @types=["int", "varchar(100)", "text", "int"], @results=#>>, @rows=0>, @trace_output=nil, @trace_mode=nil, @convert_types=true, @fetchable=true, @prepared=true, @executed=true, @cols=["id", "name", "description", "price"], @coltypes=[DBI::Type::Integer, DBI::Type::Varchar, DBI::Type::Varchar, DBI::Type::Integer], @row=[nil, nil, nil, nil], @dbh=#, @busy_handler=nil, @collations={}, @functions={}, @results_as_hash=nil, @type_translation=false, @readonly=false>, @attr={"AutoCommit"=>true, "auto_vacuum"=>true, "cache_size"=>2000, "default_cache_size"=>2000, "default_synchronous"=>nil, "default_temp_store"=>nil, "full_column_names"=>true, "synchronous"=>2, "temp_store"=>0, "type_translation"=>false}>, @trace_output=nil, @trace_mode=nil, @convert_types=true, @driver_name="SQLite3", @last_statement="select * from fruits;">, @raise_error=nil>

補足日時:2015/01/18 05:16
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!