Skip to content

Instantly share code, notes, and snippets.

@KBeltz
Created June 11, 2015 18:34
Show Gist options
  • Save KBeltz/cafb6ca10d61afc40fe9 to your computer and use it in GitHub Desktop.
Save KBeltz/cafb6ca10d61afc40fe9 to your computer and use it in GitHub Desktop.
Resource Program: Yarn
# 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