我没有使用sqlite3 gem
我正在使用mysql2 gem
我从MySQL数据库中检索数据,因为它符合某个event type
和severity
的条件。但是,它只返回一行而不是结果数组。这让我很困惑。不应该.map返回一个数组?
result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'")
equipments = result.map do |record|
[
record['sourcetime'].strftime('%H:%M:%S'),
record['equipment_id'],
record['description']
]
end
p equipments
答案 0 :(得分:1)
我误解了你的问题...我认为你在寻找的是here。
<强>更新强>
你可以改用它们,如下所示:
#!/usr/bin/env ruby
require 'mysql2'
connect= Mysql2::Client.new(:host => '', :username => '', :password => '', :database => '')
equipments = []
result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'", :symbolize_keys => true).each do |row|
equipments << [
row[:sourcetime].strftime('%H:%M:%S'),
row[:equipment_id],
row[:description]
]
end
puts "#equipments {equipments}"
编辑:
我忘了在查询结尾添加.each。所以它返回了初始化的空数组。
答案 1 :(得分:0)
您必须更改您的sql语句:
result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'", :as => :array)