2015-11-20 47 views
1
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數據庫連接問題與紅寶石使用寶石'dbi'

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

幫我看看這個。

你不使用MySQL BU的 dbi寶石

回答

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

試試這個

+0

不工作仍然得到這個錯誤。救援

':未初始化的常量Mysql(NameError) – tanay

0

...

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

嘗試做這種方式..