Android Question [Solved] Do I need set the position of ResultSet before Do While?

asales

Well-Known Member
Licensed User
In the code below, I get the result and set the positon = -1 (before the initial record) to list the records.

Do I need set the position?
B4X:
Dim rs As ResultSet
rs = SQL1.ExecQuery(txt_sql)

If rs.RowCount > 0 Then
    rs.Position = -1  'DO I NEED THIS ??
    Do While rs.NextRow
        Log(rs.GetString("id"))
    Loop
End If

rs.Close
 

DonManfred

Expert
Licensed User
Do I need set the position?
No.
B4X:
rs = SQL1.ExecQuery(txt_sql)

If rs.RowCount > 0 Then
    Do While rs.NextRow
        Log(rs.GetString("id"))
    Loop
End If
 
Upvote 2

klaus

Expert
Licensed User
Be aware that the RowCount property does exist only in B4A !
If you want to make cross-platform projects you should avoid it.
This code is enough:
B4X:
Do While rs.NextRow
    Log(rs.GetString("id"))
Loop
If RowCount = 0, Do While will not enter the Loop.
 
Upvote 3

Mahares

Expert
Licensed User
If you need or have to check for records, this will work too:
B4X:
Sub HasRecords As Boolean
    Return IIf(SQL.ExecQuerySingleResult($"SELECT count(*) FROM mytable LIMIT 1"$) = 0 , False , True )
End Sub

Log(HasRecords)  'returns true or false
 
Upvote 0
Top