reponの勉強メモ

主に勉強したことのメモです。

Sequelの使い方

ほとんどチャートシートに書いてある。
以下抜粋。

DB接続

require 'rubygems'
require 'sequel'
# connect to an in-memory database
DB = Sequel.sqlite('./testdb')

CRUD(Create、Read、Update、Delete)

Create
# create an items table
DB.create_table :items do
  primary_key :id
  String :name
  Integre :price
end


以下、datasetインスタンスを作成し操作する

# create a dataset from the items table
items = DB[:items]
insert
# populate the table
items.insert(:name => 'abc', :price => 24 * 100)
items.insert(:name => 'def', :price => 25 * 100)
items.insert(:name => 'ghi', :price => 64 * 100)
Read
# print out the number of records
puts "Item count: #{items.count}"
Update,Delete
dataset.filter(~:active).delete
dataset.filter('price < ?', 100).update(:active => true)

Drop

# drop an items table
DB.drop_table :items