W3D2: SQL part II; heredocs, sqlite3
As promised in my last post, today we learned how to embed SQL code into ruby files using the convention of heredocs. We established the relationship between our ruby files and our database, and ensured that our embedded SQL queries would return values in a hash, by creating a database class that inherited from SQLite like so:
class QuestionsDatabase < SQLite3::Database
include Singleton
def initialize
super('questions.db')
self.type_translation = true
self.results_as_hash = true
end
end
We then proceeded to create classes for each table in our data set, so that we could create instances of these classes for each individual row in the table (a User class can be used to create individual instances of User, one for each user of our program, and its relationships to other objects can be stored in instance variables.
At first, my pair partner and I didn’t intuit how to quickly test our code without actually building out a full data set, so we went through all the exercises without testing anything until our afternoon break. At that point we conferred with some neighbors and got clued in to how to run SQLite3 in our terminal in order to test specific queries, and of course a quick reminder that we could just insert values into our tables by hard-coding them in, or by calling our class methods in pry, and do plenty of testing that way. Luckily, we had a pretty good handle on how to do everything, and after churning through a couple of typo related bugs we had everything working and were feeling pretty pleased with ourselves!
Tomorrow is our first foray into a rails project (aside from the tutorial we followed in our fourth week of pre-enrollment prep work... So tomorrow’s post will be the first time this blog is living up to its name! but certainly not the last.