We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

database variables calculation in ruby

Hi everybody, in my database, am trying to print the value of total and then add it to the price,
i was able to fetch the value of total but when i tried to add it to to price so as to get value of sum in this line
of code

sum = price + %s, row[0]  
    puts "sum"

nothing happens. where am i supposed to perform the operations, is outside the loop. below is the code, any help please

#!/Ruby19/bin/ruby 
    require 'cgi'

    cgi = CGI.new
    puts cgi.header

    require "dbi"



     price = '100'
    begin
       # connect to the MySQL server
         conn = DBI.connect("DBI:Mysql:db:localhost", 
    	                    "root", "root566")

        rs = con.query("SELECT total from product where product_id = '20'")

       row = stmt.fetch do
          
       end
       puts "<font color=green size=4><b>get total values</b></font><br><br>"

     printf '<b>total:</b> %s<br>', row[0]

     sum = price + %s, row[0]  
    puts "sum"
     
       
             
    rescue Mysql::Error => e
        puts e.errno
        puts e.error
        
    ensure
        con.close if con
    end
2
Contributors
2
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
monica23
Newbie Poster
9 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Couple of things: puts "sum" just outputs the string sum not the value of the variable. If you'd like that you need to do something like puts "#{sum}" or, simply puts sum .
As for the addition, I'm not sure if you are looking to do string concatenation or a value sum. If you are looking to do concatenation then try sum = price + row[0] . If you want to add the values, then possibly sum = price.to_i + row[0].to_i (or .to_f , depending on your needs).

L7Sqr
Practically a Posting Shark
866 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7
Question Answered as of 1 Year Ago by L7Sqr

thanks a lot

monica23
Newbie Poster
9 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0641 seconds using 2.63MB