SQL: Declarative RUBY: Object oriented
Written in an object oriented language and wrapped around a relational database The object classes are mapped to the data tables in the database and the object instances are mapped to individual rows in that database
- Active Record (used a lot)
- DataMapper (used a bit)
- Sequel (pretty much not used at all)
database = Sequel.sqlite('database.sqlite3')
database.run 'CREATE TABLE people (id integer primary key autoincrement, name varchar(255))"
database.fetch "SELECT * FROM people;"
database.fetch "SELECT * from people;" do |data|
puts data[:name]
end
database[:items].select
OR
# same as above command
database.from(:items)
# select the descriptions from the items table
database.from(:items).select(:description)
# select the id and name from the items table
database.from(:items).select([:id, :name])
# Using where to scope down
database.from(:items).where(:name => 'toy')
database.from(:people).where(:first_name => 'George').where(:phone_number => '111-111-1111')
database.from(:people).where(:id => 1).update(:last_name => "Smith")
database.from(:people).where(:id => 2).delete