Created
March 1, 2017 07:42
-
-
Save Mashpy/e2c8617b87f5525d8833fa17d0aa2c06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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