It is not a true ORM
CHAPTER 8 ?– ACTIVE RECORD AND THE REAL WORLD 196
library. Your database interaction is often embedded throughout various points of your code.
You also have to do a lot of the legwork yourself, oftentimes resorting to pure SQL statements.
A DBI Example
The following example quickly shows some of the basic CRUD operations using DBI. For more
information, examples, and options for implementing DBI, you should check the library??™s official
web site.
require "dbi"
begin
# connect to DB
dbiexamp = DBI.connect("DBI:Mysql:test:localhost","root","rootpass")
# add a record
added = dbiexamp.do("insert into account(account_name) values('Kevin')")
puts "added #{added} records" # => added 1 records
# update the record
dbiexamp.do("update account set account_name = 'Kevin Marshall' where " +
"account_name = 'Kevin'")
records = dbiexamp.execute("select * from account")
records.fetch do |row|
puts "found account for #{row["account_name"]}"
end
# delete a record
dbiexamp.do("delete account where account_name = 'Kevin Marshall'")
rescue DBI::DatabaseError => error
puts "Error: #{error}"
ensure
dbiexamp.
Pages:
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453