Skip to content

Instantly share code, notes, and snippets.

@Mashpy
Created March 1, 2017 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mashpy/e2c8617b87f5525d8833fa17d0aa2c06 to your computer and use it in GitHub Desktop.
Save Mashpy/e2c8617b87f5525d8833fa17d0aa2c06 to your computer and use it in GitHub Desktop.
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