While running this script, it produces the following result at our Linux machine. # get server version string and display it You have gone through MySQL tutorial to understand MySQL Basics.įollowing is the example of connecting with MySQL database "TESTDB"ĭbh = DBI.connect("DBI:Mysql:TESTDB:localhost", "testuser", "test123") Ruby Module DBI is installed properly on your machine. User ID "testuser" and password "test123" are set to access TESTDB. This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX, and INCOME. $ ruby setup.rb config -with = dbi,dbd_mysqlįinal step is to build the driver and install it using the following commands −Īssuming we are going to work with MySQL database, before connecting to a database make sure of the following − For example, to configure only the main DBI module and the MySQL DBD-level driver, issue the following command − To be more specific, provide a -with option that lists the particular parts of the distribution you want to use. This command configures the distribution to install all drivers by default. The most general configuration command looks like this, with no arguments following the config argument. Go in distribution directory dbi-0.2.0 nd configure it using the setup.rb script in that directory. Now, follow the steps given below − Step 1 You can install ruby DBI using the Ruby Gems packaging manager:īefore starting this installation make sure you have the root privilege. This module acts as a DBD as explained above and can be downloaded from Obtaining and Installing Ruby/DBI If you want to write Ruby scripts to access MySQL databases, you'll need to have the Ruby MySQL module installed. Each driver interprets requests from the DBI layer and maps them onto requests appropriate for a given type of database server. There is one driver for MySQL, another for PostgreSQL, another for InterBase, another for Oracle, and so forth. This layer is database dependent different drivers provide access to different database engines. This layer is database independent and provides a set of common access methods that are used the same way regardless of the type of database server with which you're communicating. The general architecture for Ruby DBI uses two layers − This is clear from the following architecture diagram. You can use DBI whether you are working with Oracle, MySQL or Informix, etc. It defines a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used.ĭBI is independent of any database available in the backend. The Ruby DBI module provides a database-independent interface for Ruby scripts similar to that of the Perl DBI module.ĭBI stands for Database Independent Interface for Ruby, which means DBI provides an abstraction layer between the Ruby code and the underlying database, allowing you to switch database implementations really easily. This chapter teaches you how to access a database using Ruby.
0 Comments
Leave a Reply. |