Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'sqlite3'
begin
db = SQLite3::Database.open "test.db"
db.transaction
db.execute "DROP TABLE IF EXISTS animal"
db.execute "CREATE TABLE animal
(
name CHAR(40),
category CHAR(40)
)"
db.execute "INSERT INTO animal (name, category)
VALUES
('snake', 'reptile'),
('frog', 'amphibian'),
('tuna', 'fish'),
('racoon', 'mammal')"
db.commit
res = db.query('select * from animal')
res.each_hash {|h| puts h['name']}
rescue SQLite3::Exception => e
puts "Exception occurred"
puts e
db.rollback
ensure
res.close if res
db.close if db
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.