返回列表

Ruby on Rails –'require': no such file to load -- mysql (LoadError)

默认分类 2011-03-30 18:37:20

require 'rubygems' require 'mysql'

begin
dbh = Mysql.real_connect("localhost", "root", "", "book",3306)

创建表,插入计录,查询计录

dbh.query("drop table if exists test_rb")

dbh.query("create table test_rb(id int,name char(20))")

dbh.query("insert into test_rb values(1,'张三'),(2,'李四')")

printf "%d rows were inserted

",dbh.affected_rows

res = dbh.query("SELECT * FROM books")
puts "======== "
while row = res.fetch_row do
printf "%s, %s ", row[1], row[2]
end
puts "======== "
puts "Server version: " + dbh.get_server_info
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
end