Created
June 11, 2015 18:34
-
-
Save KBeltz/cafb6ca10d61afc40fe9 to your computer and use it in GitHub Desktop.
Resource Program: Yarn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# empower my program with SQLite | |
require "sqlite3" | |
# load/create our database for this program | |
DATABASE = SQLite3::Database.new("stash.db") | |
DATABASE.results_as_hash = true | |
DATABASE.execute("CREATE TABLE IF NOT EXISTS yarn (id INTEGER PRIMARY KEY, name TEXT, fiber TEXT, weight TEXT, yards INTEGER, price INTEGER);") | |
def yarn | |
DATABASE.execute('SELECT * FROM yarn;') | |
end | |
def add_yarn(yarn_name, fiber_type, yarn_weight, num_yards, skein_price) | |
DATABASE.execute("INSERT INTO yarn (name, fiber, weight, yards, price) VALUES ('#{yarn_name}', '#{fiber_type}', '#{yarn_weight}', '#{num_yards}', '#{skein_price}');") | |
end | |
def change_yarn_name(new_yarn_name, id) | |
DATABASE.execute("UPDATE yarn SET name = '#{new_yarn_name}' WHERE id = #{id};") | |
end | |
def student_name(id) | |
result = DATABASE.execute("SELECT name FROM yarn WHERE id = #{id};") | |
result.first["name"] | |
# result[0]["name"] | |
end | |
def delete_yarn(id) | |
DATABASE.execute("DELETE FROM yarn WHERE id = #{id};") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment