Issues while connecting to mysql using ruby
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
error i am geting is:
undefined method `connect' for Mysql:Class (NoMethodError)
OR
undefined method `real_connect' for Mysql:Class (NoMethodError)
EDIT return value of 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?"]
return value of Mysql.methods(false)
is []... blank array
EDIT2
mysql.rb file
# 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
I had this same problem and solved this way:
make sure you have installed only the gem ruby-mysql and not the gem mysql. For me, now:
$ gem list --local | grep mysql
ruby-mysql (2.9.2)
If that is not the case, uninstall
$ sudo gem uninstall mysql
(I uninstalled every gem with mysql in its name) and then reinstalled ruby-mysql.
In my case, because I have mysql installed in a usb disk the installation command was:
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
(and I was using the 32bits binary for MacOs, don't know if that applies for you)
Finally, my ruby test program was
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
Short answer:
- Remove
mysql-ruby
- Rebuild
mysql-ruby
- Reinstall
mysql-ruby
.
Alternative answer
- Remove
mysql-ruby
- Install
ruby-mysql
The pure ruby MySQL protocol client.
Longer Explanation:
This just happened to me. My 2.8.1 mysql-ruby
bindings had been built against libmysqlclient.so.15
, and worked fine until I upgraded my MySQL installation and replaced that client library with .so.16
. Rebuild resolved this issue.
The 3rd-party gem you used (I used it, too) introduces faulty logic in the mysql.rb
file it supplies to trap an error on Windows systems. Notice that in the excerpt you posted, that this mysql.rb
file does not re-raise the LoadError
on non-Windows platforms. Bummer.
Edit
I contacted the gemspec author, and he has corrected the error! (2010-05-25) With luck no one else will be baffled by this silent failure.
精彩评论