I’m pretty sure that the Ruby DBI gem is deprecated, however, I’m writing NORM so that I can experience the pain of refactoring as well as proving out that Test Driven Design has a place in modern development. It also helps because previous to the project I’m currently working on for my job at SolutionStream I have not used TDD and I’m anxious to see its power in a project like this one.
I found I needed the MySQL adapter for my application—I’m only building for MySQL right now. That required the dbd-mysql gem in addition to the dbi gem. After installing the gems, I found I could require them and then use them to connect to the database. Here are some code snippets from NORM that show how I’m using the DBI library.
First require the dbi and mysql gems.
require 'dbd/mysql' require 'dbi'
Then, in my object, I’m storing the connection in a class variable.
@@connection = DBI.connect("DBI:Mysql:norm:localhost", "root", "")
Finally, I’m using the connection to perform operations on the database.
def create(attributes)
columns = attributes.keys.join(", ")
values = attributes.collect {|k, v| "'#{v}'"}.join(", ")
@@connection.execute("INSERT INTO base (#{columns}) VALUES (#{values});")
new(attributes)
end




