我必须使用Ruby程序以下面指定的方式显示数据。 有人可以帮助我,我该怎么做。必须在SQlcode或Ruby代码中进行更改。 代码如下:
require 'sqlite3'
#db=SQLite3::Database.open ('adaptive.db')
class Database
@@db=SQLite3::Database.open 'adaptive.db'
def registered_users
@@db.execute(
"Select strftime('%Y-%m',created_at) AS Month, count(uuid) as RegistredPeopleNo
from users
group by strftime('%Y-%m',created_at)
order by Month;") do |row|
p row
end
end
def registered_devices
@@db.execute(
"Select strftime('%Y-%m',created_at) AS RegisteredMonth,
count(serial_number) AS RegisteredDevicesNo
from devices
group by strftime('%Y-%m',created_at)
order by RegisteredMonth;") do |row|
p row
end
end
def unregistered_users
@@db.execute(
"Select strftime('%Y-%m',updated_at) AS Unregister, count(id) AS Id
from users
where id not in(Select user_id from devices)
group by strftime('%Y-%m',updated_at)
order by Unregister;") do |row|
p row
end
end
def unregistered_devices
@@db.execute(
"Select strftime('%Y-%m',updated_at) AS Unregister,count(id) AS Id
from devices
where channel_id is null
group by strftime('%Y-%m',updated_at);") do |row|
p row
end
end
def device_per_user
@@db.execute(
"select register AS NoOFMachines,count(id) as NoOFPeople
from(
Select users.id AS id,count(devices.id) AS register
from users,devices
where users.id=devices.user_id
group by devices.user_id)
group by register;") do |row|
p row
end
end
end
运行命令:
>irb
>require './database'
>db=Database.new
>db.registered_users#
The output is following:
["2015-08",23]
["2015-09",102]
["2015-10",59]
["2015-11",25]
但是,我希望它显示如下:
[["2015-08",23],["2015-09",102],["2015-10",59],["2015-11",25]]
有人可以帮我解决这个问题......提前致谢
答案 0 :(得分:1)
将值存储在Array中。 试试这个:
def registered_users
arr = []
@@db.execute(
"Select strftime('%Y-%m',created_at) AS Month, count(uuid) as RegistredPeopleNo
from users
group by strftime('%Y-%m',created_at)
order by Month;") do |row|
arr << row
end
p arr
end
更新:您不需要each
循环。可能在代码下面会给你你想要的结果。
@@db.execute(
"Select strftime('%Y-%m',created_at) AS Month, count(uuid) as RegistredPeopleNo
from users
group by strftime('%Y-%m',created_at)
order by Month;")`