Ruby Mysql Connectivity

This post shows you how you can connect to MySQL in Ruby.   I’m assuming that you already aware with basic SQL Operation like how to create database, table and insert, delete, update statement. So I’m directly dive in coding part, as I believe that more theory can confuse us. so I gonna explain you straight to the point.

okay so first we try to connect to the MySQL server and fetch all rows from table “test”.

require 'mysql'
con ='localhost', 'root', 'root', 'test')  
rs = con.query('select * from demo')
rs.each_hash do |row|
    puts row['uid'] + " " + row['name']

to run this basic example you need to use following command

ruby fetchallrow.rb

for insert statement you can use following code.

con.query("INSERT INTO demo values('"+id+"','"+name+"','"+mno+"')")

now you can able to perform basic insert,update and delete statement in same way.

That’s It.!

Enjoy d code.. 🙂


Leave a Reply

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

You are commenting using your 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