我在 ruby 1.8.7中执行以下代码来从我的数据库中读取行: require 'dbi'db_conn_handle = DBI.connect("DBI:Mysql:host=localhost;database=mydb;port=3306", "root")sth = db_conn_handle.prepare("select accounts.id, accounts.name
require 'dbi' db_conn_handle = DBI.connect("DBI:Mysql:host=localhost;database=mydb;port=3306", "root") sth = db_conn_handle.prepare("select accounts.id, accounts.name from accounts;") sth.execute info = sth.to_a puts "Info: #{info[0].class}" info.each do |x, y| puts "#{x} ... #{y}" end
从输出中可以清楚地看出,info [0] .class是DBI :: Row.使用ruby 1.8.7(rails 3.2.17)执行此代码时效果很好
当我尝试在ruby 2.1.5 / rails 3.2.17中执行它时,它会出现以下错误:
/home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:392:in `__getobj__': not delegated (ArgumentError) from /home/rjain/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/delegate.rb:341:in `block in delegating_block' from /home/rjain/mac/query.rb:7:in `each' from /home/rjain/mac/query.rb:7:in `<top (required)>' from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `eval' from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands/runner.rb:52:in `<top (required)>' from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `require' from /home/rjain/.rvm/gems/ruby-2.1.5/gems/railties-3.2.17/lib/rails/commands.rb:64:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
文件/home/rjain/mac/query.rb粘贴在上面.我想了解ruby 2.1和1.8之间导致此问题的区别.这个问题的解决方法是什么?
陷入同样的问题,追查问题.在gem目录中找到lib / dbi / row.rb. 212行或左右应阅读
if RUBY_VERSION =~ /^1\.9/
编辑它
if RUBY_VERSION =~ /^1\.9/ || RUBY_VERSION =~ /^2/