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

Preparing the SQL Statuments

  1: sql = <<-SQL
  2: SELECT email
  3: FROM person, email
  4: WHERE person.personid = email.personid
  5: AND person.nick = ? 
  6: ORDER BY nick, type
  7: SQL
  8: 
  9: statement = db.prepare(sql)

  • [1-6] Define the SQL command using a "here" document.
    • Notice the ? placeholder for the (as of yet) unknown value to be used for the nickname.

  • [9] Prepare the SQL statement
    • SQL statements that will be repeatably executed with many data values should be prepared before executing.
    • Preparation allows the SQL engine to optimize the query strategy for once for a statement (this could be expensive).


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