【发布时间】:2013-09-06 09:45:59
【问题描述】:
我想使用 Ruby CGI 脚本在浏览器中显示我的 MySQL 数据库中的数据。
我遇到的问题是显示数据;它只显示 Title 列,并且只显示一个单元格,Price 和 ISBN 列不显示任何内容。
我使用“Title varchar, Price decimal(10,2), ISBN integer”来创建表格。
我尝试先显示价格和 ISBN,但这两列甚至不打印,但数据在数据库中。
#!/usr/bin/env ruby
require 'mysql2'
require 'cgi'
client = Mysql2::Client.new(
:host => "localhost",
:database => "tempdb",
:username => "user",
:password => "pass"
)
results = client.query("SELECT * FROM mytable")
cgi = CGI.new
puts cgi.header
puts "<table border='1'>
<tr>
<th>Title</th>
<th>Price</th>
<th>ISBN</th>
</tr>"
results.each do |row|
puts "<tr>"
puts "<td>" + row["Title"] + "</td>"
puts "<td>" + row["Price"] + "</td>"
puts "<td>" + row["ISBN"] + "</td>"
puts "</tr>"
end
puts "</table>";
【问题讨论】:
-
我只是需要一些东西来显示我自己的数据,我不想使用任何花哨的东西。但是,我肯定会看看 Sinatra 和 Sequel。谢谢!