使用gem'dbi'与ruby进行Mysql DB连接的问题

时间:2015-11-20 06:00:58

标签: mysql ruby database 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

并显示错误。

  

救援':未初始化的常量Mysql(NameError)。

帮助我。

2 个答案:

答案 0 :(得分:0)

你没有使用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

尝试这样做..

答案 1 :(得分:0)

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

试试这个