1

I connect to database and change db.results_as_hash to true

db = SQLite3::Database.new 'barbershop.sqlite'
db.results_as_hash = true

But it don't display hash, it is simple array. And don't work

<% @results.each do |row| %>
    <tr>
        <td><%= row['Name'] %></td> 

Working code is

<% @results.each do |row| %>
    <tr>
        <td><%= row[1] %></td>

Error is "no implicit conversion of String into Integer"

What's wrong?

Ruby 2.1.5p273, SQLite version 3.8.5, MacOS X Yosemite

Evgeny Palguev
  • 599
  • 3
  • 18
  • I don't see anything wrong with your code can you post the actual section of code that sets `@results`. e.g. the controller action if this is rails (can't tell because you are using erb but you are also setting the database explicitly) – engineersmnky Aug 13 '15 at 15:38
  • I use Sinatra, erb. @results = db.execute 'SELECT * FROM Users ORDER BY Id' . May be I must install some gem? – Evgeny Palguev Aug 13 '15 at 15:42
  • 1
    what do you mean by "And don't work"? Is there an error? Are you sure that the column is named "Name" (uppercase?) or could it be "name" (lowercase?). There is no additional dependancy for this feature although it is suggested in the [docs](http://sqlite-ruby.rubyforge.org/sqlite3/faq.html#538670736) that using [`arrayfields`](https://github.com/ahoward/arrayfields) is an option as well. – engineersmnky Aug 13 '15 at 15:45
  • Error is "no implicit conversion of String into Integer". Name of column does not influence – Evgeny Palguev Aug 13 '15 at 15:46
  • Okay that clearly indicates your assumption of an Array is correct although I still cannot find fault with your code. maybe try using arrayfields and see if that helps. – engineersmnky Aug 13 '15 at 15:49
  • Thank you! May be is a bug of sqlite3 or MacOS X? – Evgeny Palguev Aug 13 '15 at 15:52
  • Can you attach the code that sets `@results`? – Nick Veys Aug 13 '15 at 15:59
  • <% @results.each do |row| %> <%= row[1] %> <%= row[2] %> <%= row[3] %> <%= row[4] %> <%= row[5] %> <% end %> – Evgeny Palguev Aug 13 '15 at 16:07
  • Nope, the code that *sets* `@results`. The query, most likely. – Nick Veys Aug 13 '15 at 16:08
  • @results = db.execute 'SELECT * FROM Users ORDER BY Id DESC' - ? – Evgeny Palguev Aug 13 '15 at 16:10

1 Answers1

3

From the repo you posted. You're not actually setting that flag. The code in your question is not the code you're running. There's no db variable, and you return on the first line, so the second never runs.

def get_db
  return SQLite3::Database.new 'barbershop.sqlite'
  db.results_as_hash = true
end

I'm guessing that only compiles because the last line isn't executing. Try this:

def get_db
  db = SQLite3::Database.new 'barbershop.sqlite'
  db.results_as_hash = true
  db
end
Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • nice catch I did not even see a repo posted ou that would have immediately reviewed the source. BTW since this is ruby there is no compilation as this would be interpreted in real time (thus no error because it never interprets the erroneous line) – engineersmnky Aug 13 '15 at 18:51