Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Forked from anonymous/database.rb
Created April 12, 2011 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberfox/916442 to your computer and use it in GitHub Desktop.
Save cyberfox/916442 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sqlite3'
require 'sequel'
# Adding this monkey-patch makes it work on MacRuby.
module Sequel
class Dataset
def single_record
record = nil
clone(:limit=>1).each{|r| record = r; return r}
record
end
end
end
DB = Sequel.sqlite()
DB.create_table :entries do
primary_key :id
String :words
end
DB[:entries].insert(:words => "Hello MacRuby!")
puts "Querying for all entries records: #{DB[:entries].all.to_s}"
puts "Query for just the first record: #{DB[:entries].first}"
puts "Query for record based on id: #{DB[:entries].where(:id => 1).first.to_s}"
#results
# $ macruby database.rb
# Querying for all entries records: [{:id=>1, :words=>"Hello MacRuby!"}]
# Query for just the first record:
# Query for record based on id:
# $ rvm use 1.9.2
# $ ruby database.rb
# Querying for all entries records: [{:id=>1, :words=>"Hello MacRuby!"}]
# Query for just the first record: {:id=>1, :words=>"Hello MacRuby"}
# Query for record based on id: {:id=>1, :words=>"Hello MacRuby!"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment