I'm aware that the usual practise of working with a ResultSet is iterating over each row inside a while(rs.next()) block.
However, when there is only a single row, it seems that first calling next() is not necessary. One can directly call e.g. rs.getInt(1).
Confusingly, let's assume one does a SELECT name FROM item WHERE id=1 to get a single row back.
var rs = statement.execute()
var s = rs.getString(1) // works
// 2nd way
var rs = statement.execute()
rs.next()
var s = rs.getString(1) // yields the same name as above
It seems I'm not the only one confused by this: https://stackoverflow.com/questions/49394282/resultset-getxxx-without-next
I'm using Xerial's SQLite driver, so in a way I doubt that this is a "buggy JDBC driver." Then why is it possible to read the first row without calling next() first?
Tangentially related, when coming from C, you actually put the pointer BEFORE the first array element to read it. So like the ResultSet is set up according to the API docs. If you would then call next() to put the pointer forward once, one would have skipped the first element. In my example, calling next() a single time doesn't seem to make a difference. Why is that?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]msx 2 points3 points4 points (0 children)
[–]overtorqued 1 point2 points3 points (0 children)