[ next ] [ prev ] [ contents ] Using Ruby

Ruby's Data Base Interface (DBI)

require 'dbi'

db = DBI.connect("DBI:Pg:poc", 'jim', nil)
sql = %{
  SELECT name, id
  FROM users
  WHERE id < 4
}
db.select_all(sql) do |row|
  puts "User #{row['name']}, id #{row['id']}"
end
db.disconnect

Output

User jim, id 1
User dave, id 2
User rob, id 3

  • Ruby DBI is a vender independent Database Interface

  • Similar to Java's JDBC, but modeled after Perl's DBI.

  • Takes advantage of Ruby's features to make DBI even easier to use.

  • Convenience functions select_all,select_one, and do handle the prepare, execute and close operations internally.

  • Statements can be individully prepared if desired.


[ next ] [ prev ] [ contents ] Copyright 2003 by Jim Weirich.
Some Rights Reserved