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 = Mysql.new('localhost', 'root', 'root', 'test') rs = con.query('select * from demo') rs.each_hash do |row| puts row['uid'] + " " + row['name'] end con.close
to run this basic example you need to use following command
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.
Enjoy d code.. 🙂