Richard Burton
Richard Burton

Reputation: 2240

Ruby mysql gem error

I have install the mysql gem but am falling at the first hurdle:

 initialize': wrong number of arguments(4 for 0) (ArgumentError)
 from open.rb:14:in `new' 
 from open.rb:14:in `<main>'

Is the result from this code:

 require 'mysql'

 db = Mysql.new('localhost','root','','test')
 puts db

I was following the code from this tutorial:

http://rubylearning.com/satishtalim/ruby_mysql_tutorial.html

It just looks like the new method is not going to accept 4 arguments. I have no idea why. Mysql.new creates a new object just fine.

Upvotes: 1

Views: 579

Answers (2)

alf
alf

Reputation: 18530

Use the Mysql2 gem:

gem install mysql2

Then:

require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root")
results = client.query("show databases")
results.each do |row|
  puts row["Database"]
end

You can find more information in the gem documentation

HTH!

Upvotes: 2

Nicholas Pufal
Nicholas Pufal

Reputation: 2205

Is line 14 the line where you declare db = Mysql.new('localhost','root','','test')?

I've just opened irb, installed the gem, copied your line and it worked just fine - it returned me a Mysql object as expected.

Can you try do the same via irb? Btw, I recommend you to install the mysql2 gem instead. Here is an explanation about the why: What the difference between mysql and mysql2 gem

Upvotes: 0

Related Questions