Advertisement
Guest User

Untitled

a guest
Aug 19th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # create table a (i int not null, str text not null);
  2. # insert into a (i, str) values (23, "bai bai");
  3.  
  4. require "sqlite3"
  5. require "http/server"
  6.  
  7.  
  8. class Test
  9. getter i
  10. getter str
  11.  
  12. def self.by_id(db, id)
  13. # rs = db.query("SELECT i, str FROM a WHERE i = ?", 23)
  14. # return nil unless rs.next
  15. # mod = Test.new(rs["i"] as Int, rs["str"] as String)
  16. # rs.close
  17. # return mod
  18.  
  19. db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
  20. rs.next
  21. return Test.new(rs["i"] as Int, rs["str"] as String)
  22. end
  23. end
  24.  
  25. def initialize(@i, @str)
  26. end
  27.  
  28. def update(db)
  29. db.execute("UPDATE a SET str = ? WHERE i = ?", Time.now.to_s, 23) do |rs|
  30. p rs
  31. end
  32. end
  33. end
  34.  
  35. 2.times do
  36. db = SQLite3::Database.new("/tmp/test.db")
  37. begin
  38. t = Test.by_id(db, 23)
  39. if t
  40. t.update(db)
  41. db.close
  42. end
  43. ensure
  44. db.close
  45. end
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement