欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ruby连接数据库_学习备份

程序员文章站 2022-06-08 10:00:20
...
先安装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