-
-
Save zeroc0d3/92cc762f396809c8bfe5a11daf53ffba to your computer and use it in GitHub Desktop.
example of using the ruby sequel gem to access a sqlite3 database on jruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################################################# | |
| # 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