– Ruby – Mysql – ActiveRecord –

Hi friends 🙂

Mysql connection with ruby

Today Mr.Thyagarajan Shanmugam sir told us about Mysql connection with ruby .

He told that we can connect with two ways , one by normal Query and another way is ORM

ORM – Object Relation Mapping

He told uses of ORM ,

  • If u are going to change database other than MySQL we can easily change in ORM .. because we dont use

SQL Query inside that ruby code .

Sir gave a small task on that .. He asked to  make ruby program which will get “sales_amount” and

“sales_comission”  from USER .

He asked to do in Two methods :-

Normal ruby program which connects with MySql

require 'mysql'
db = Mysql.new("localhost","root","sathia","suresh")
print "Enter the sales amount :"
a = gets.chomp.to_f
print "Enter the sales comission :"
b = gets.chomp.to_f
entry = db.prepare "INSERT INTO salesdetails (sales_amount , sales_comission) VALUES (#{a} , #{b})"

Using  Active Record

Active method is Module available in ruby

Installation :-  sudo gem install activerecord

same program by this method is –

require 'rubygems'
 require 'active_record'
 :adapter => "mysql",
 :host => "localhost",
 :database => "suresh" ,
 :password => "sathia"
 class Salesdetail < ActiveRecord::Base
print "Enter the sales amount :"
a  = gets.chomp.to_f
print "Enter the sales comission :"
b = gets.chomp.to_f

 Salesdetail.create(:sales_amount => a , :sales_comission => b )


About sathia

Web developer at cloudmint
This entry was posted in programs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s