user6558246
user6558246

Reputation:

ruby/sql syntax undefined method `query' for nil:NilClass (NoMethodError)

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 !

Upvotes: 2

Views: 1500

Answers (3)

br3nt
br3nt

Reputation: 9586

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

Upvotes: 2

Berlin
Berlin

Reputation: 1464

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: '')
 }

Upvotes: 0

Nic Nilov
Nic Nilov

Reputation: 5156

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.

Upvotes: 0

Related Questions