Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # create table a (i int not null, str text not null);
- # insert into a (i, str) values (23, "bai bai");
- require "sqlite3"
- require "http/server"
- class Test
- getter i
- getter str
- def self.by_id(db, id)
- # rs = db.query("SELECT i, str FROM a WHERE i = ?", 23)
- # return nil unless rs.next
- # mod = Test.new(rs["i"] as Int, rs["str"] as String)
- # rs.close
- # return mod
- db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
- rs.next
- return Test.new(rs["i"] as Int, rs["str"] as String)
- end
- end
- def initialize(@i, @str)
- end
- def update(db)
- db.execute("UPDATE a SET str = ? WHERE i = ?", Time.now.to_s, 23) do |rs|
- p rs
- end
- end
- end
- 2.times do
- db = SQLite3::Database.new("/tmp/test.db")
- begin
- t = Test.by_id(db, 23)
- if t
- t.update(db)
- db.close
- end
- ensure
- db.close
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement