【问题标题】:Issue in Mysql DB connection with ruby using gem 'dbi'使用 gem 'dbi' 与 ruby​​ 的 Mysql DB 连接中的问题
【发布时间】:2016-02-22 13:03:49
【问题描述】:
    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 ': 未初始化的常量 Mysql (NameError)。

帮我解决这个问题。

【问题讨论】:

    标签: mysql ruby database dbi


    【解决方案1】:

    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

    试试这个

    【讨论】:

    • 不工作仍然出现此错误。在
      ' 中救援:未初始化的常量 Mysql (NameError)
    【解决方案2】:

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

    试试这样吧..

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 1970-01-01
      • 2017-01-11
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多