Repeating hash information with faulty keys

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

···

--
Posted via http://www.ruby-forum.com/.

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

Fascinating. Thanks for the suggested solution, Joel. I am curious to
know why the hash from #view_library1 includes duplicate information.
Any thoughts as to why that might be?

···

--
Posted via http://www.ruby-forum.com/.

Setting results_as_hash gives you a hash with both column name and column
index as keys into the fields in each record returned. The data is not
duplicated in the database, but shows up twice in the hash with the alpha
and numeric indeces.

  p books[0][0] # => 1
  p books[0]['id'] #=> 1

  p books[0][1] # => "The Spirit Catches You and You Fall Down"
  p books[0]['title'] # => "The Spirit Catches You and You Fall Down"

This is what setting results_as_hash does, in fact. If you wanted each
record to be an array of column values, you'd set results_as_hash false.

···

On Mon, Dec 16, 2013 at 7:48 PM, Mike Vezzani <lists@ruby-forum.com> wrote:

Fascinating. Thanks for the suggested solution, Joel. I am curious to
know why the hash from #view_library1 includes duplicate information.
Any thoughts as to why that might be?

--
Posted via http://www.ruby-forum.com/\.