2

I am having trouble just trying to pull data out of my table. I just want to pull the integer value from column Diff and add/subtract numbers to it.

Once I am done adding/subtracting, I want to update each row with the new value"

My Table chart for "users" in ruby

My Table chart for "users" in ruby

This is my code as of now

require 'date'
require 'mysql2'
require 'time' 

def test()
  connect = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "rubydb")
  result = connect.query("SELECT * FROM users where Status='CheckOut'")
  if result.count > 0
    result.each do |row|
    stored_diff = connect.query("SELECT * FROM users WHERE Diff")
    #stored_diff = stored_diff.to_s  
    puts stored_diff
    end
  end
end

test()

I am sure the code in the hashtag does not work since I am getting like #Mysql2::Result:0x000000000004863248 etc. Can anyone help me with this?

3
  • Diff column is set as int already Commented Jul 25, 2018 at 19:06
  • 2
    That's your resultset, it seems perfectly okay. I wanted to help you but it's not clear to me what you want to achieve. Commented Jul 25, 2018 at 19:24
  • @Ursus I want to update the existing "Diff" value by adding this to it. 'time_change = ((Time.parse(row['CheckOutDateTime'].to_s) - Time.parse(row['CheckInDateTime'].to_s)) / 60).round' So row 1's "Diff" will add it's original value to time_change Commented Jul 25, 2018 at 19:39

1 Answer 1

0

I have no knowledge of but I'll show you the steps to achieve what you are trying based on this and this.

  1. Get User Ids and the Diff numbers.

    SELECT `Id`, `Diff` FROM users where `Status`='CheckOut'
    
  2. Iterate the result.

    result.each do |row|
    
  3. Assign Diff and Id into variables.

    usrId = #{row['Id']};
    diffCal = #{row['Diff']};
    
  4. Do your calculations to diffCal variable.

  5. Execute the UPDATE query.

    UPDATE `users` SET `Diff` = '#{diffCal}' WHERE `Id` = '#{usrId}'
    
Sign up to request clarification or add additional context in comments.

2 Comments

how would you assign Diff into a variable?
Yeah, sry about that

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.