Skip to content

Instantly share code, notes, and snippets.

@xfalcox
Created November 18, 2016 22:44
Show Gist options
  • Save xfalcox/5639a9b4e7014d0e8c5f1074d161668e to your computer and use it in GitHub Desktop.
Save xfalcox/5639a9b4e7014d0e8c5f1074d161668e to your computer and use it in GitHub Desktop.
#antes use gem install sqlite3 sequel
require 'sequel'
DB = Sequel.connect('sqlite://blog.db') # requires sqlite3
DB.create_table :items do
primary_key :id
String :name
Float :price
end
items = DB[:items] # Create a dataset
# Populate the table
items.insert(:name => 'abc', :price => rand * 100)
items.insert(:name => 'def', :price => rand * 100)
items.insert(:name => 'ghi', :price => rand * 100)
# Print out the number of records
puts "Item count: #{items.count}"
# Print out the average price
puts "The average price is: #{items.avg(:price)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment