You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When not using the rs.each block to read each row sequentially, it is seemingly necessary for rs.move_next to be called before the returned data can be accessed.
This doesn't seem to be documented in any visible fashion and lead to half an hour of staring at my query trying to figure out what was wrong with my code.
As an example, the following causes read the column <column> returned a Nil but a Int64 was expected.:
DB.open <uri> do |db|
db.query"SELECT <column> FROM <table> WHERE <column> = <value>"do |rs|
putsrs.read(Int32)endend
By contrast, this code returns the expected value, even if the table only has a single row:
DB.open <uri> do |db|
db.query"SELECT <column> FROM <table> WHERE <column> = <value>"do |rs|
rs.move_next# <-- Added lineputsrs.read(Int32)endend
As I mentioned before, this seems to imply that calling rs.move_next or use of the rs.each block is mandatory for the query results to be populated, which unless I'm misunderstanding something seems a unintuitive.
If this intended, please document it somewhere easily visible to save future trial and error sessions from other potential users.
The text was updated successfully, but these errors were encountered:
OpenSauce04
changed the title
Reading a single row from a database is weird and unintuitive
Reading a single row from a table is weird and unintuitive
Dec 18, 2024
OpenSauce04
changed the title
Reading a single row from a table is weird and unintuitive
Data from db.query is not populated until rs.move_next is called
Jan 2, 2025
When not using the
rs.each
block to read each row sequentially, it is seemingly necessary forrs.move_next
to be called before the returned data can be accessed.This doesn't seem to be documented in any visible fashion and lead to half an hour of staring at my query trying to figure out what was wrong with my code.
As an example, the following causes
read the column <column> returned a Nil but a Int64 was expected.
:By contrast, this code returns the expected value, even if the table only has a single row:
As I mentioned before, this seems to imply that calling
rs.move_next
or use of thers.each
block is mandatory for the query results to be populated, which unless I'm misunderstanding something seems a unintuitive.If this intended, please document it somewhere easily visible to save future trial and error sessions from other potential users.
The text was updated successfully, but these errors were encountered: