Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created September 10, 2014 07:14
Show Gist options
  • Select an option

  • Save memememomo/73059e2d301f8d295f58 to your computer and use it in GitHub Desktop.

Select an option

Save memememomo/73059e2d301f8d295f58 to your computer and use it in GitHub Desktop.
mysql2-cs-bind + sql-maker で、MySQLを操作するスクリプトを書く ref: http://qiita.com/uchiko/items/899aeb85ac408f254545
# A sample Gemfile
source "https://rubygems.org"
gem 'sql-maker'
gem 'mysql2-cs-bind'
require 'sql-maker'
require 'mysql2-cs-bind'
DB_HOST = 'localhost'
DB_USER = 'root'
DB_PASS = ''
DB_NAME = 'hogehoge'
builder = SQL::Maker.new(:driver => 'mysql')
dbh = Mysql2::Client.new(
:host => DB_HOST,
:username => DB_USER,
:password => DB_PASS,
:database => DB_NAME
)
# SELECT
sql, binds = builder.select('users', ['*'], {:id => 1})
dbh.xquery(sql, binds).each do |row|
p row
end
# INSERT
sql, binds = builder.insert('users', {:name => 'hanako'})
dbh.xquery(sql, binds)
# UPDATE
sql, binds = builder.update('users', {:name => 'tarou'}, {:id => 1})
dbh.xquery(sql, binds)
# DELETE
sql, binds = builder.delete('users', {:id => 1})
dbh.xquery(sql, binds)
$ bundle exec -- ruby sample.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment