Hm, the following works for me...
require 'rubygems'
require 'sqlite3'
$db = SQLite3::Database.open('books.db')
$db.results_as_hash = true
def view_library1
puts "view_library1"
books = $db.execute("SELECT * FROM books")
puts books
end
def view_library2
puts "view_library2"
books = $db.execute("SELECT * FROM books")
books.each do |b|
puts %Q{Title: #{b['title']}
Author: #{b['author']}}
end
end
view_library1
view_library2
__END__
Output:
view_library1
{"id"=>1, "title"=>"The Spirit Catches You and You Fall Down", "author"=>"Anne Fadiman", "year"=>1997, "publisher"=>"Farrar, Straus, and Giroux", "isbn"=>"0-374-52564-1", 0=>1, 1=>"The Spirit Catches You and You Fall Down", 2=>"Anne Fadiman", 3=>1997, 4=>"Farrar, Straus, and Giroux", 5=>"0-374-52564-1"}
{"id"=>2, "title"=>"Bringing It To The Table", "author"=>"Wendell Berry", "year"=>2009, "publisher"=>"Counterpoint", "isbn"=>"978-1-58243-543-5", 0=>2, 1=>"Bringing It To The Table", 2=>"Wendell Berry", 3=>2009, 4=>"Counterpoint", 5=>"978-1-58243-543-5"}
view_library2
Title: The Spirit Catches You and You Fall Down
Author: Anne Fadiman
Title: Bringing It To The Table
Author: Wendell Berry
···
On 12/16/2013 02:21 PM, Mike Vezzani wrote:
Hi all,
I'm creating a command line library program for tracking which books I
own. I've decided to use SQLite3 for data persistence. I've included the
necessary files to run my program as it stands right now.
The issue I'm having is that there is unwanted duplication in my
#view_library results hash, and I haven't the slightest clue why.
If you run library_client.rb from the command line and type '1' as your
choice, the output shows duplication in the books.db file. I'm not sure
if this has something to do with line 9: $db.results_as_hash = true or
not.
I've commented out the code as I would like to have it between lines 20
and 24, but right now it doesn't function properly because of the
duplicate information that has integers as hash keys.
As always, any help is greatly appreciated!
~Mike V.
Attachments:
http://www.ruby-forum.com/attachment/9070/library_client.rb
http://www.ruby-forum.com/attachment/9071/books.db