RubyでMySQLに接続する方法
方法1:
require “mysql"
dbc = Mysql.real_connect('127.0.0.1′,’root’,’startnews24_pwd’,’startnews24_test’)
res = dbc.query('select * from users’)
while row = res.fetch_row do
puts “#{row[0]},#{row[1]}"
end
方法2:
require 'mysql2’
client = Mysql2::Client.new(:host => “ホスト名", :username => “ユーザー名",:password=>"パスワード",:database=>"データベース")
results = client.query(“select * from テーブル");
results.each do |hash|
puts hash.map { |k,v| “#{k} = #{v}" }.join(“, “)
end