39
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyでMySQLに接続

Last updated at Posted at 2014-05-23

MySQLのgemをインストールしておく。

gem install mysql

Rubyでの書き方

require 'mysql'

connection = Mysql::connect("hostname", "username", "password", "dbname")

# 文字コードをUTF8に設定
connection.query("set character set utf8")

# DBに問い合わせ
rs = connection.query("SELECT * FROM users")

# 検索結果を表示
rs.each do |r|
  puts r.join ", "
end

# コネクションを閉じる
connection.close

追記
mysqlのgemのインストールでエラーが出た場合はこちらが参考になるかと思います。
http://qiita.com/azusanakano/items/329cbf517797ade2c8b0

39
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?