W3D2 - Object Relational Mapping & SQLite3
ORM - Object Relational Mapping
Build our own from scratch
SQLite3 - serverless, can only run 1 query at a time, only good for small scale apps
Creating a SQL database from an import_db.sql file
Seeding a DB using INSERT INTO in import_db.sql file
SQLite3::Database - setting up & using a SQLite3 DB connection
Heredocs - doesn’t matter what you put after <<-, will start on following line and end once it meets the same string as starter
QuestionsDatabase.instance.execute(<<-SQL)
Treats entire block as a string
Can use interpolation #{}
a/A Application to help handle questions from students
QuestionsDatabase class < SQLite3::Database
self.type_translation = true – maintains original type of input/values
self.results_as_hash = true – can set results via hash
Wrote a ton of class methods to pull relationships, save instances to the DB, set relationships, etc.
class ModelBase that other models inherit, to modularize ::find_by_id(id), ::all, and #save methods
class Question < ModelBase
Seems similar to what ActiveRecord does (tomorrow’s project)
Methods that ActiveRecord has that are similar/identical to the ones we built
::new #name = " " #save ::first ::all ::last #destroy ::find(id) ::find_by(key: value) ::create(key1: value1, key2: value2)
7. Partner: Ken Cha * Finished entire project, half of the bonus problems
cat import_db.sql | sqlite3 questions.db - command to create DB
gem ‘sqlite3’ - must include in Gemfile to use sqlite3
Became more comfortable writing SQL queries using heredocs
Use class methods to write instance methods
Joseph Kim knows Javascript
Matt Corley - can write code blocks using < pre > < code > Put code block here!!! < / code > < / pre > to write code blocks on separate lines on Tumblr
Dawn Tran-Kiem uses memes in her a/A blog posts. They are hilarious.
Samuel Lyons is following my blog – W00T!!!
If you want to build up a SQL query as a string and then include it as an argument in a SQLite3::Database.instance.execute(query) method, must include ’ around string values, otherwise will throw error
SQLite3::Database.instance methods
#execute - will return an array of rows (hashed data)
#get_first_row - will return only first row
#get_first_value - will return only first row, first column returned. Ideal if only returning one aggregated value