-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbtesting.rb
More file actions
54 lines (34 loc) · 1.29 KB
/
dbtesting.rb
File metadata and controls
54 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require "rubygems"
require "sequel"
require 'sqlite3'
# connect to an in-memory database
DB = Sequel.sqlite
# create an items table
DB.create_table :items do
primary_key :id
String :name
Float :price
Float :time
end
# create a dataset from the items table
items = DB[:items]
# populate the table
items.insert(:name => 'abc', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'def', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'ghi', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'jkl', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'mnop', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'qrs', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'tuv', :price => rand * 100, :time => Time.now.getutc)
items.insert(:name => 'wxy', :price => rand * 100, :time => Time.now.getutc)
# print out the number of records
puts "Item count: #{items.count}"
puts "Average time is: #{items.avg(:time)}"
result = items.time.find(:name, :order => "id desc", :limit => 5)
while !result.empty?
puts result.pop
end
my_posts = DB[:time].filter(:name => 'abc')
puts "test print out #{my_posts}"
# print out the average price
puts "The average price is: #{items.avg(:price)}"