[ next ] [ prev ] [ contents ] [ up to Appendix B -- Database Example ] Invitation To Ruby

Creating a Database Connection

To use to a database, we first must create a database connection.

  1: require 'dbi'
  2: 
  3: db = DBI.connect(
  4:   "DBI:Pg:jim",
  5:    user, 
  6:    password)
  7: # Use the database
  8: db.disconnect # When done

  • [1] Require the DBI library.

  • [3-6] Create a database connection. The DBI module interprets the "DBI:xxx:yyy" string to determine the Type of database ("Pg" in this example) and the database (e.g. "jim").
    • The DBI software will find the correct DBD driver for the DBI string and automatically load it.

  • [8] Disconnect from the database when you are done.


[ next ] [ prev ] [ contents ] [ up to Appendix B -- Database Example ] Copyright 2002 by Jim Weirich.
All rights reserved.