Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Task
  2. DB = PG.connect :hostaddr => "127.0.0.1", :port => 5432, :dbname => 'testdb', :user => "postgres", :password => "postgres"
  3. #uncomment while deploying to heroku
  4. #DB = PG.connect ENV["HEROKU_POSTGRESQL_SILVER_URL"]
  5. # uncomment to create pg database
  6. # DB = PG.connect(hostaddr: "127.0.0.1", port: 5432, dbname: 'postgres', user: 'postgres', password: "postgres")
  7. # DB.exec("CREATE DATABASE testdb")
  8.  
  9. # uncomment to create table
  10. # DB.exec "DROP TABLE IF EXISTS tasks"
  11. # DB.exec "CREATE TABLE tasks(Id SERIAL PRIMARY KEY, Name VARCHAR(20), Finished INT)"
  12.  
  13. class << self
  14. def all()
  15. DB.exec("select * from tasks")
  16. end
  17.  
  18. def update(task, finish, id)
  19. DB.exec("UPDATE tasks set name = '#{task}', finished = '#{finish}' where id='#{id}'")
  20. end
  21.  
  22. def save(task_name)
  23. DB.exec("INSERT INTO tasks (name, finished) VALUES ('#{task_name}',0)")
  24. end
  25.  
  26. def delete(id)
  27. DB.exec("delete from tasks where id='#{id}'")
  28. end
  29.  
  30. def where(id)
  31. DB.exec("select * from tasks where id = '#{id}'")
  32. end
  33. end
  34.  
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement