Skip to content

Instantly share code, notes, and snippets.

@TGSmith
Last active May 3, 2017 11:34
Show Gist options
  • Save TGSmith/2b54167c214413955ede to your computer and use it in GitHub Desktop.
Save TGSmith/2b54167c214413955ede to your computer and use it in GitHub Desktop.
Solution for Student Roster DB from Schema
# Solution for Challenge: Student Roster DB from Schema. Started 2013-07-09T19:29:26+00:00
require 'sqlite3'
# If you want to overwrite your database you will need
# to delete it before running this file
$db = SQLite3::Database.new "students.db"
module StudentDB
def self.setup
$db.execute(
<<-SQL
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name VARCHAR(64) NOT NULL,
last_name VARCHAR(64) NOT NULL,
gender VARCHAR(64) NOT NULL,
birthday DATE NOT NULL,
email VARCHAR(64) NOT NULL,
phone VARCHAR(64) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);
SQL
)
end
def self.seed
# Add a few records to your database when you start
$db.execute(
<<-SQL
INSERT INTO students
(first_name, last_name, gender, birthday, email, phone, created_at, updated_at)
VALUES
('Brick','Thornton', 'male', '1983-03-13', '[email protected]', '123-456-7890', DATETIME('now'), DATETIME('now'));
-- Create two more students who are at least as cool as this one.
SQL
)
end
end
class Students
def initialize
@created_at = Time.now
@updated_at = Time.now
end
def add_student(first_name, last_name, gender, birthday, email, phone)
$db.execute(
<<-SQL
INSERT INTO students
(first_name, last_name, gender, birthday, email, phone, created_at, updated_at)
VALUES
("#{first_name}", "#{last_name}", "#{gender}", "#{birthday}", "#{email}", "#{phone}", "#{@created_at}", "#{@updated_at}");
SQL
)
end
def delete_student(student_id)
$db.execute(
<<-SQL
DELETE FROM students WHERE id = "#{student_id}"
SQL
)
end
def list_students
$db.execute(
<<-SQL
SELECT * FROM students
SQL
)
end
def first_name(first_name)
$db.execute(
<<-SQL
SELECT * FROM students WHERE first_name="#{first_name}"
SQL
)
end
def by_attribute(attribute, value)
$db.execute(
<<-SQL
SELECT * FROM students WHERE "#{attribute}"="#{value}"
SQL
)
end
def birthdays_this_month
current_month = Time.now.month.to_s
current_month.prepend('0') if current_month.length == 1
p current_month
$db.execute(
<<-SQL
SELECT * FROM students WHERE birthday LIKE "%-#{current_month}-%"
SQL
)
end
def list_by_birthday
$db.execute(
<<-SQL
SELECT birthday, first_name, last_name FROM students
GROUP BY birthday
ORDER BY birthday
SQL
)
end
end
student = Students.new
student.add_student("Jake", "Myers", "male", "1979-12-01", "[email protected]", "859-457-0990")
student.add_student("Anew", "Record", "male", "1979-07-01", "[email protected]", "555-777-0888")
student.delete_student(2)
p student.list_students
p student.first_name("Jake")
p student.by_attribute("last_name", "Myers")
p student.birthdays_this_month
p student.list_by_birthday
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment