Skip to content

Instantly share code, notes, and snippets.

@cayblood
Created December 6, 2011 21:54
Show Gist options
  • Save cayblood/1440190 to your computer and use it in GitHub Desktop.
Save cayblood/1440190 to your computer and use it in GitHub Desktop.
example of using the ruby sequel gem to access a sqlite3 database on jruby
#############################################################################
# to use this in jruby:
#
# jgem install sequel
# jgem install jdbc-sqlite3
#
# for more info on the sequel library, go to http://sequel.rubyforge.org/
#############################################################################
require 'rubygems'
require 'sequel'
DB = Sequel.connect('jdbc:sqlite:db.sqlite3')
unless DB.table_exists?(:items)
DB.create_table :items do
column :name, :text
column :price, :float
end
end
items = DB[:items]
items << {:name => 'abc', :price => rand * 100}
items << {:name => 'def', :price => rand * 100}
items << {:name => 'ghi', :price => rand * 100}
puts "Item Count = #{items.count}"
items.reverse_order(:price).each do |item|
p item
end
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