0

I'm not using sqlite3 gem
I'm using mysql2 gem

I'm retrieving data from MySQL database given that it meets the condition of a certain event type and severity. However, it returns only one row instead of an array of results. It really puzzles me. Shouldnt .map return an array?

   result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'") 
    equipments = result.map do |record| 
        [
            record['sourcetime'].strftime('%H:%M:%S'), 
            record['equipment_id'], 
            record['description']
        ]
    end
    p equipments
6
  • Possible duplicate of Return Values of #execute and #query methods of sqlite3 gem Commented Mar 23, 2018 at 6:38
  • @JollyProgrammer I'm using MySQL not sqlite3 Commented Mar 23, 2018 at 6:52
  • You are using DBI right? The interface should be the same for both databases. Commented Mar 23, 2018 at 9:09
  • @JollyProgrammer I'm not sure what DBI is, but one thing for sure is that I dont use $db in my code. I'm just querying MySQL using Ruby. Commented Mar 23, 2018 at 9:22
  • what is the connect object in your code? DBI stands for Database Independent Interface for Ruby. Commented Mar 23, 2018 at 9:43

2 Answers 2

1

I had misread your question...I think what you are looking for is in here.

UPDATE

You can use each instead, like this:

#!/usr/bin/env ruby
require 'mysql2'

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

equipments = []
result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'", :symbolize_keys => true).each do |row|
    equipments << [ 
                    row[:sourcetime].strftime('%H:%M:%S'), 
                    row[:equipment_id], 
                    row[:description] 
                  ]
end

puts "#equipments {equipments}"

EDITED:

I forgot to add .each at the end of the query. So it was returning the initialized empty array instead.

Sign up to request clarification or add additional context in comments.

9 Comments

pluck is not Ruby Core, what do you require ?
Consider replacing this by a duplicate note.
How do I do that? I am new here :)
@Yunnosch What do you mean by that?
@JoshuaNg At the time of my comment, the answer just read "Look in this other answer", with a link. That kind of answer is unneeded and should be replaced by a flag as duplicate. The answer has changed now and seems now to contain more than just the link, which I assume is different from the linked answer. This answer can now be evaluated by its own content.
|
0

You must need to change your sql statement :

result = connect.query("SELECT * FROM data WHERE event_type = 'ALARM_OPENED' AND severity = '2'", :as => :array)

3 Comments

I get an error saying no implicit conversion of String into Integer. I tried adding .to_s but the error still persists
The problem with :as => :array is that it will return each row as an array. So when you try to get the values like record['equipment_id'] it tries to convert 'equipment_id' to an integer and crashes. Can you try to run the original query that you had with this line after: result.count?
@JollyProgrammer The count is 1

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.