require 'rubygems'
require 'mysql'
db = Mysql.connect('localhost', 'root', '', 'mohit')
//db.rb:4: undefined method `connect' for Mysql:Class (NoMethodError)
//undefined method `real_connect' for Mysql:Class (NoMethodError)
db.query("CREATE TABLE people ( id integer primary key, name varchar(50), age integer)")
db.query("INSERT INTO people (name, age) VALUES('Chris', 25)")
begin
query = db.query('SELECT * FROM people')
puts "There were #{query.num_rows} rows returned"
query.each_hash do |h|
puts h.inspect
end
rescue
puts db.errno
puts db.error
end
错误我正在寻找:
undefined method `connect' for Mysql:Class (NoMethodError)
OR
undefined method `real_connect' for Mysql:Class (NoMethodError)
修改 返回值Mysql.methods
["private_class_method", "inspect", "name", "tap", "clone", "public_methods", "object_id", "__send__", "method_defined?", "instance_variable_defined?", "equal?", "freeze", "extend", "send", "const_defined?", "methods", "ancestors", "module_eval", "instance_method", "hash", "autoload?", "dup", "to_enum", "instance_methods", "public_method_defined?", "instance_variables", "class_variable_defined?", "eql?", "constants", "id", "instance_eval", "singleton_methods", "module_exec", "const_missing", "taint", "instance_variable_get", "frozen?", "enum_for", "private_method_defined?", "public_instance_methods", "display", "instance_of?", "superclass", "method", "to_a", "included_modules", "const_get", "instance_exec", "type", "<", "protected_methods", "<=>", "class_eval", "==", "class_variables", ">", "===", "instance_variable_set", "protected_instance_methods", "protected_method_defined?", "respond_to?", "kind_of?", ">=", "public_class_method", "to_s", "<=", "const_set", "allocate", "class", "new", "private_methods", "=~", "tainted?", "__id__", "class_exec", "autoload", "untaint", "nil?", "private_instance_methods", "include?", "is_a?"]
返回Mysql.methods的值(false)
是[] ...空白数组
EDIT2
mysql.rb文件
# support multiple ruby version (fat binaries under windows)
begin
require 'mysql_api'
rescue LoadError
if RUBY_PLATFORM =~ /mingw|mswin/ then
RUBY_VERSION =~ /(\d+.\d+)/
require "#{$1}/mysql_api"
end
end
# define version string to be used internally for the Gem by Hoe.
class Mysql
module GemVersion
VERSION = '2.8.1'
end
end
答案 0 :(得分:7)
我遇到了同样的问题并以这种方式解决了:
确保您只安装了gem ruby-mysql 而不是宝石mysql。对我来说,现在:
$ gem list --local | grep mysql
ruby-mysql(2.9.2)
如果不是这样,请卸载
$ sudo gem uninstall mysql
(我用名字中的mysql卸载了每个gem) 然后重新安装ruby-mysql。
在我的情况下,因为我在usb磁盘中安装了mysql 安装命令是:
sudo env ARCHFLAGS="-arch i386" gem install ruby-mysql --
--with-mysql-config=/Volumes/usb/opt/bin/osx/mysql/bin/mysql_config
--with-mysql-lib=/Volumes/usb/opt/bin/osx/mysql/lib/
--with-mysql-dir=/Volumes/usb/opt/bin/osx/mysql
(我使用32位二进制文件用于MacO,不知道这是否适用于你)
最后,我的ruby测试程序是
require 'rubygems'
require 'mysql'
dbh = Mysql.real_connect('localhost', 'root', 'your password', 'TEST')
res = dbh.query("select * from Persons;");
puts res.class
res.each do |row|
puts row.join(" ")
end
答案 1 :(得分:1)
简答:
mysql-ruby
mysql-ruby
mysql-ruby
。替代答案
mysql-ruby
ruby-mysql
更长的说明:
这恰好发生在我身上。我的2.8.1 mysql-ruby
绑定是针对libmysqlclient.so.15
构建的,并且工作正常,直到我升级MySQL安装并用.so.16
替换了该客户端库。重建解决了这个问题。
您使用的第三方gem(我也使用过它)在它提供的mysql.rb
文件中引入了错误逻辑,以便在Windows系统上捕获错误。请注意,在您发布的摘录中,此mysql.rb
文件不在非Windows平台上重新引发LoadError
。长号。
修改强>
我联系了gemspec作者,他有corrected the error! (2010-05-25)幸运的是,没有人会被这种沉默的失败困惑。