ruby连接数据库_学习备份

先安装ruby对mysql的支持库,
gem install mysql
require 'rubygems'
require 'mysql'

db = Mysql.connect('localhost','root','','book')

db.query("insert into bookinfos (bookname,type) values('soft','2')");

begin
  query = db.query('select * from bookinfos')
  puts "There were #{query.num_rows} rows returned"
  
  query.each_hash do |h|
    puts h.inspect
  end
rescue
  puts db.errno
  puts db.error
end

你可能感兴趣的:(Ruby)