使用 gem 'dbi' ruby 的 Mysql 数据库连接问题

Issue in Mysql DB connection with ruby using gem 'dbi'

    gem 'dbi'
require 'dbi'


begin
con = Mysql.new 'localhost', 'root', '1234'
puts con.get_server_info
rs = con.query 'SELECT VERSION()'
puts rs.fetch_row    

rescue Mysql::Error => e
puts e.errno
puts e.error

ensure
con.close if con
end

它显示这样的错误。

rescue in ': uninitialized constant Mysql (NameError).

帮我解决这个问题。

您没有使用 MySQL 但 dbi gem...

dbh = DBI.connect('DBI:Mysql:localhost', 'testuser', 'testpwd')
sth = dbh.prepare('SELECT VERSION()')
sth.execute
while row=sth.fetch do
    p row
end

尝试这样做..

require 'rubygems'
require 'mysql'
require 'dbi'


begin
 con = DBI.connect("DBI:Mysql:localhost","username", "password")
puts con.get_server_info
rs = con.query 'SELECT VERSION()'
puts rs.fetch_row    

rescue Mysql::Error => e
puts e.errno
puts e.error

ensure
con.close if con
end

试试这个