Last active
December 11, 2015 18:08
-
-
Save syun77/4639260 to your computer and use it in GitHub Desktop.
SQLite/Rubyのサンプル
This file contains 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
#!/usr/bun/ruby | |
# ■メモリから新規作成 | |
require 'rubygems' | |
require 'sqlite3' | |
# メモリから | |
db = SQLite3::Database::new(':memory:') | |
# SQL作成 | |
sql =<<SQL | |
CREATE TABLE skill( | |
id integer, | |
name varchar(10) | |
); | |
SQL | |
# SQL実行 | |
db.execute(sql) | |
# INSERT | |
sql = "INSERT INTO skill values(1, 'fire')" | |
db.execute(sql); | |
# パラメータ付きINSERT | |
sql = "INSERT INTO skill values(?, ?)" | |
db.execute(sql, 2, 'ice') | |
db.execute(sql, 10, 'quake') | |
# レコード確認 | |
db.execute('SELECT * FROM skill') do |row| | |
puts row.join("\t") | |
end |
This file contains 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
#!/usr/bun/ruby | |
# ■ファイルを指定して読み込み | |
require 'rubygems' | |
require 'sqlite3' | |
db = SQLite3::Database::new("test.db") | |
db.execute('SELECT * FROM skill') do |row| | |
puts row.join("\t") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment