New release seem to brake "results as hash"

153 views
Skip to first unread message

Johannes Held

unread,
Jul 10, 2010, 1:16:19 AM7/10/10
to sqlite3-ruby
Hi, with the last update, I can't index the resultset via column names
anymore.

This is an examplary irb-session:

>> require 'sqlite3'
=> true
>> db = SQLite3::Database.new("/home/jo/dokumente/Ausgaben/ausgaben.db")
=> #<SQLite3::Database:0x000000012e05d8>
>> db.type_translation = true
=> true
>> stmt = "select * from ausgaben where monat = 7"
=> "select * from ausgaben where monat = 7"
>> row = db.get_first_row(stmt)
=> [2010, 7, "jo", 245.93, "miete zimmer", false]
>> row.fields
=> ["jahr", "monat", "name", "betrag", "tags", "gemeinsam"]
>> row.columns
NoMethodError: undefined method `columns' for [2010, 7, "jo", 245.93,
"miete zimmer", false]:SQLite3::ResultSet::ArrayWithTypesAndFields
from (irb):7
from /usr/bin/irb:12:in `<main>'
>> db.results_as_hash = true
=> true
>> row = db.get_first_row(stmt)
NoMethodError: undefined method `columns' for #<Array:
0x0000000132fa98>
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:134:in `block
(2 levels) in execute'
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:132:in `each'
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:132:in `block
in execute'
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:87:in
`prepare'
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:125:in
`execute'
from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:274:in
`get_first_row'
from (irb):9
from /usr/bin/irb:12:in `<main>'
>>

Hoping that's only a simple bug - or that I used your library wrong!
Please tell me, if I should provide more information.

Johannes Held

Aaron Patterson

unread,
Jul 10, 2010, 12:44:51 PM7/10/10
to sqlite...@googlegroups.com
On Fri, Jul 9, 2010 at 10:16 PM, Johannes Held <heh...@gmail.com> wrote:
> Hi, with the last update, I can't index the resultset via column names
> anymore.
>
> This is an examplary irb-session:
>
>>> require 'sqlite3'
> => true
>>> db =  SQLite3::Database.new("/home/jo/dokumente/Ausgaben/ausgaben.db")
> => #<SQLite3::Database:0x000000012e05d8>
>>> db.type_translation = true
> => true

As a work around, try *not* setting type_translation.
SQLite3::Database#type_translation= is being deprecated anyway.

>>> stmt = "select * from ausgaben where monat = 7"
> => "select * from ausgaben where monat = 7"
>>> row = db.get_first_row(stmt)
> => [2010, 7, "jo", 245.93, "miete zimmer", false]
>>> row.fields
> => ["jahr", "monat", "name", "betrag", "tags", "gemeinsam"]
>>> row.columns
> NoMethodError: undefined method `columns' for [2010, 7, "jo", 245.93,
> "miete zimmer", false]:SQLite3::ResultSet::ArrayWithTypesAndFields
>        from (irb):7
>        from /usr/bin/irb:12:in `<main>'

This object does not respond to columns.

>>> db.results_as_hash = true
> => true
>>> row = db.get_first_row(stmt)
> NoMethodError: undefined method `columns' for #<Array:
> 0x0000000132fa98>
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:134:in `block
> (2 levels) in execute'
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:132:in `each'
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:132:in `block
> in execute'
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:87:in
> `prepare'
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:125:in
> `execute'
>        from /usr/lib/ruby/site_ruby/1.9.1/sqlite3/database.rb:274:in
> `get_first_row'
>        from (irb):9
>        from /usr/bin/irb:12:in `<main>'

The above call should work if you don't set type_translation to true.

> Hoping that's only a simple bug - or that I used your library wrong!
> Please tell me, if I should provide more information.

This was enough information to reproduce the bug, thanks! For now you
should be able to work around the problem by not setting type
translation. I've fixed the bug here:

http://github.com/luislavena/sqlite3-ruby/commit/685a7f1126aac82572de79d3d8b06023f850b418

But please note that SQLite3::Database#type_translation= is being deprecated.

--
Aaron Patterson
http://tenderlovemaking.com/

Reply all
Reply to author
Forward
0 new messages