2

I keep getting the following error message:

.rb:215:in block in collectData': undefined methodquery' for nil:NilClass (NoMethodError)

This is my code:

databases = {
  'aaa': Mysql2::Client.new(
      host:     '',
      username: '',
      password: '',
      database: ''
    ),
  'bbb': Mysql2::Client.new(
      host:     '',
      username: '',
      password: '',
      database: ''
    )
}

client1.query(" bla bla bla").each do |row|
  if row['mmm'] == 99
    # Look in next db
    next_db = databases[row['db']]
    row2 = next_db.query("SELECT...")

I take the name of the db from my first sql query, client1.query, for example row['db'] = "aaa" or "bbb".

For the test something like this also not working:

client1.query(" bla bla bla").each do |row|
  if row['mmm'] == 99
    # Look in next db
    next_db = databases['aaa']
    row2 = next_db.query("SELECT...")

And something like this works:

client1 = Mysql2::Client.new(
    host:     '',
    username: '',
    password: '',
    database: ''
  )

client2 = Mysql2::Client.new(
    host:     '',
    username: '',
    password: '',
    database: ''
  )

client1.query(" bla bla bla").each do |row|
  if row['mmm'] == 99
    # Look in next db
    next_db = client2
    row2 = next_db.query("SELECT ")

I can't seem to understand why my method is "undefined".

Please help,

Thanks !

3 Answers 3

2

So you have your hash of databases, and you want to query the next database based off the result from the first database query...

Basically the code you have is right except you never check for nil. In this case, whatever is being returned by row['db'], a matching database is not found in the databases hash. I'm going to assume that this is an exception that needs to be fixed.

So...

# hash of databases
databases = {
   'aaa' => Mysql2::Client.new(host: '', username: '', password: '', database: ''),
   'bbb' => Mysql2::Client.new(host: '',username: '', password: '', database: '')
 }

# get the first database
client1 = databases['aaa'] 

# perform the query
client1.query("bla bla bla").each do |row|
  if row['mmm'] == 99
    # look in next db
    client2 = databases[row['db']]

    if (client2)
      row2 = client2.query("SELECT...")
    else
      raise "Database `#{row['db']}` is not defined."
    end
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

I still got the same error "Database aaa is not defined", I fixed that with the answer above, thank you.
@AlexBrickman oops, was using the wrong syntax for the hash... Fixed now
The colon was converting the keys to symbols, therefore databases[row['db']] was returning nil as it was finding trying to find the db by a string, not a symbol.
0

try to change the hash of databases to:

databases = {
   'aaa' => Mysql2::Client.new(host: '', username: '', password: '', database: ''),
   'bbb' => Mysql2::Client.new(host: '',username: '', password: '', database: '')
 }

2 Comments

its works ! but why with => and not with : , is someone can explain?
please see @br3nt explanation.
0

Your client instances reside not in the client variable like in your working example but within a hash. The reason for the error is that you're trying to call query method on a variable that has nil value.

You should access your clients appropriately:

databases['aaa'].query("
  ").each do |row|
            if row['mmm'] == 99
              # Look in next db
                next_db = client
                row2 = next_db.query("
                SELECT " )

Of course to achieve the logic you're aiming for, it will be necessary to walk the databases hash as you do the steps.

4 Comments

thanks, but I need to access the db by the name that I get from my first sql query, row['db'] can be aaa or bbb.
i want to access to aaa and bbb by the name that i get from my first query.
Then write a recursive function, with each step using appropriate client of the ones contained in the databases. This aspect is not related to your original question which has received an answer.
I asked here to get some help how should I do it, because even if I do something like this "next_db = databases['aaa']" it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.