1.安装mysql驱动
gem install mysql
irb
irb(main):001:0> require 'mysql' //测试安装
2.使用
require 'mysql'
begin
puts "connect to mysql database"
dbh = Mysql.real_connect("localhost", "root", "root","chj", 3306)
//连接数据库本机:用户名:root 密码:root 数据库:chj 端口:3306
puts "connect success"
puts "==============="
#dbh.query("drop table if exists member")
#dbh.query("create table member(memberId int,email varchar(25),aliww varchar(30))")
#dbh.query("insert into member values(1,'abc@163.com,'zhangsan')")
#printf "%d rows were inserted/n",dbh.affected_rows
res = dbh.query("SELECT memberId,email,aliww FROM member")
while row = res.fetch_row do
puts "#{row[0]} | #{row[1]} | #{row[2]}"
end
rescue Mysql::Error=>e
puts "Error code:#{e.errno}"
puts "Error message:#{e.error}"
puts "Error SQLSTATE:#{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
dbh.close if dbh
puts "close the connection"
end
阅读(2773) | 评论(0) | 转发(0) |