To view this screencast, add it to
your cart
and
checkout.
You can buy this screencast for any price, including FREE!
Sequel is an awesome Ruby library for working with databases.
As opposed to ActiveRecord and DataMapper, which are based on working with model objects, Sequel is meant to be used to directly query a database, without needed to go through an object relational mapper. You can use models and associations with Sequel, but you don’t have to.
If you’re a DBA, you work with legacy databases, or you simply enjoy working directly with databases … Sequel is for you!
Here are a few examples … for more info, check out the Sequel website or watch the screencast!
Connect to a Sqlite3 database and print out its tables
1 >> require 'sequel' 2 => true 3 >> DB = Sequel.sqlite 'dogs.db' 4 => #<Sequel::SQLite::Database: "sqlite:/dogs.db"> 5 >> DB.tables 6 => [:toys, :dogs]
Create
1 >> DB[:toys].insert :name => 'foooooooo' 2 => 4
Read
1 >> DB[:toys].first 2 => {:name=>"bar", :id=>2} 3 >> DB[:toys].where(:name => 'foo').first 4 => {:name=>"foo", :id=>3} 5 >> DB[:toys].where(:name.like('%f%')).all 6 => [{:name=>"foo", :id=>3}, {:name=>"foooooooo", :id=>4}] 7 >> DB[:toys].where(:name.like('%f%')).exclude(:id => 4).all 8 => [{:name=>"foo", :id=>3}]
Update
1 >> DB[:toys].where(:id => 3).first 2 => {:name=>"foo", :id=>3} 3 >> DB[:toys].where(:id => 3).update :name => 'Not Foo' 4 => 1 5 >> DB[:toys].where(:id => 3).first 6 => {:name=>"Not Foo", :id=>3}
Delete
1 >> DB[:toys].where(:id => 3).delete 2 => 1 3 >> DB[:toys].where(:id => 3).first 4 => nil