2015-04-11

After you install Ruby and build the Rails framework, you need to create the mysql gem. This blog post shows you how to create the mysql gem and how to write a simple Ruby program that queries the MySQL database.

The first step creates the mysql gem for Ruby programming:

It should show you the following:

After you install the mysql Ruby Gem, you can write and test a test.rb Ruby program that tests a MySQL database connection. The simplest complete code looks like this:

You can run the program with the following syntax:

The program prints “Connected to the MySQL database server.” when there’s a student user with a student password that’s authorized to connect to the studentdb database. If any of the values are invalid when creating the connection, the program prints “Can’t connect to the MySQL database specified.”

Having tested the connection, the next query.rb program tests the connection by returning values from a query:

You can test it with the following command-line syntax:

It returns a data set like this from the item table of my video store example:

As always, I hope this helps those trying to get things working.

PlanetMySQL Voting: Vote UP / Vote DOWN

Show more