Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data from db.query is not populated until rs.move_next is called #101

Open
OpenSauce04 opened this issue Dec 18, 2024 · 0 comments
Open

Comments

@OpenSauce04
Copy link

OpenSauce04 commented Dec 18, 2024

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|
    puts rs.read(Int32)
  end
end

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 line
    puts rs.read(Int32)
  end
end

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.

@OpenSauce04 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant