Created
September 10, 2014 07:14
-
-
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
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
| # A sample Gemfile | |
| source "https://rubygems.org" | |
| gem 'sql-maker' | |
| gem 'mysql2-cs-bind' |
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
| 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) |
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
| $ bundle exec -- ruby sample.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment