Created
December 15, 2010 08:40
-
-
Save noqisofon/741776 to your computer and use it in GitHub Desktop.
bit.ly/hsUTKm の「差集合で関係除算を表現する」チャプターで使うテーブルを Ruby/SQLite3 で作成してみました。
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
#!c/bin/ruby/bin/ruby | |
# -*- encoding: cp932 -*- | |
# file: testinit.rb | |
require 'sqlite3' | |
# データベースを作成します。 | |
# 直ぐに空のデータベースファイル employees.db ができます。 | |
# 既に存在する場合は、読み込むだけです。 | |
db = SQLite3::Database.new( "employees.db" ) | |
# めんどいので、一度にテーブルを 2 つ作ることにします。 | |
create_query = <<-SQL | |
create table Skills ( | |
skill_id int primary key, | |
skill_name text not null | |
); | |
create table EmpSkills ( | |
empskill_id int primary key, | |
employee_name text not null, | |
skill_name text not null | |
); | |
SQL | |
# 複数ステートメントを実行する場合は、execute_batch メソッドを使います。 | |
db.execute_batch( create_query ) | |
# | |
# Skills の方のレコードを挿入します。 | |
# | |
# 名前付きプレースホルダーも指定できます。 | |
sql_query = <<-SQL | |
insert into Skills ( skill_name ) values ( :skill_name ) | |
SQL | |
# 処理を連続して行う場合は、transaction ブロック内で一度にすべて処理しときます。 | |
db.transaction do | |
[ "Oracle", "UNIX", "Java" ].each do |content| | |
# skill_id は primary key なので、挿入するごとに 1 足されていきます。 | |
db.execute( sql_query, :skill_name => content ) | |
end | |
end | |
sql_query = <<-SQL | |
insert into EmpSkills ( employee_name, skill_name ) values ( :employee_name, :skill_name ) | |
SQL | |
db.transaction do | |
# | |
# EmpSkills の方のレコードを挿入します。 | |
# | |
{ "相田" => [ "Oracle", "UNIX", "Java", "C#" ], | |
"神崎" => [ "Oracle", "UNIX", "Java" ], | |
"平井" => [ "UNIX", "Oracle", "PHP", "Perl", "C++" ], | |
"若田部" => [ "Perl" ], | |
"渡来" => [ "Oracle" ] | |
}.each do |k, v| | |
v.each do |x| | |
db.execute( sql_query, :employee_name => k, :skill_name => x ) | |
end | |
end | |
end | |
# データベースを閉じます。 | |
db.close |
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
#!c/bin/ruby/bin/ruby | |
# -*- encoding: cp932 -*- | |
# file: testdivide.rb | |
Encoding::default_external = Encoding::UTF_8 | |
require 'sqlite3' | |
# データベースを作成します。 | |
# 直ぐに空のデータベースファイル employees.db ができます。 | |
# 既に存在する場合は、読み込むだけです。 | |
db = SQLite3::Database.new( "employees.db" ) | |
sql_query = <<-SQL | |
select distinct employee_name | |
from | |
EmpSkills as ES1 | |
where | |
not exists | |
(select skill_name | |
from | |
Skills | |
except | |
select skill_name | |
from | |
EmpSkills as ES2 | |
where | |
ES1.employee_name = ES2.employee_name) | |
SQL | |
db.execute( sql_query ) do |row| | |
puts row.map { |cell| cell.encode( "shift_jis" ) }.join( "," ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment