Ruby, Ruby Tutorial

Ruby – Connect to MySQL without Rails

This post demonstrates how to connect to MySQL from Ruby, insert, select and delete table rows.

Installation Details

These are the stuff currently installed on my Windows 10 box.

  1. Notepad++
  2. Ruby
    • ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-mingw32]
  3. Gem 2.6.7
  4. mysql2 (install gem using the command below)

    For more information on mysql2, visit https://github.com/brianmario/mysql2
  5. eventmachine (install gem using the command below)

Install the gems

MySQL Server Details

You will be connecting to a MySQL Server and therefore it has to be installed first. Then, create a database schema rubydb.

Database Table

Create a database table persons using the following SQL DDL

Our Ruby Codes

Run the script

To run the script from the command-line, use the following command.

Output

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like