mysqlsqlrubydashing

SQL Error with ruby


require 'mysql2'

SCHEDULER.every '2h', :first_in => 0 do |job|

  # MySQL connection
  db = Mysql2::Client.new(:host => "host", :username => "username", :password => "password", :port => port, :database => "database" ) 

  sql = "select count(*) from tickets where department_id = 6;"
  tickets_sql = db.query(sql)
  puts tickets_sql

  #Send Events
  send_event('tickets_sql',    {current: tickets_sql})


end

I am using the puts command so I can see the output in my log file. The output of this command is an error message which is: #<Mysql2::Result:0x000000025546a8>

What does this error mean?

I took the same exact query as in the code and ran it on the database and it outputs the number as expected.


Solution

  • It is not an error message, it is an object (Mysql2::Result). You can give count(*) and alias (put as smth after it) and access as tickets_sql.first['smth'] (you can use first as there is only one row, otherwise you get a collection, so you should iterate through it with .each, for example, to output retrieved rows with puts).