Ruby MySQL Tutorial

This brief Ruby MySQL Tutorial shows you how you can connect to MySQL in Ruby. MySQL support in Ruby was made possible by Tomita Masahiro. He has developed a pure Ruby binding called Ruby/MySQL.

We need to install the same on our PC and the installation (you need to be connected to the internet and it takes some time) is as shown below:

This installs mysql-2.7.3-mswin32 driver which is faster and supports MySQL 5.0.51a and later. Also, it requires Ruby 1.8.6. The documentation for this driver is here.

I will assume that you've already installed MySQL 5 or above on your PC and that you have it running and are familiar with the basics.

First, ensure that your MySQL server is running. If not, you can start it by typing this at the command prompt:

Now, open a new command window run the mysql client program from the command line, as:

You should get the mysql prompt. Next, create a database ruby as:

Next, create a table student in the database ruby as:

Populate the above table with some data and as a first exercise we try to connect to the MySQL server and print all the names in the table student. Program p078rubymysql.rb

It is as simple as that. You can explore this api further.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them.