B4J Question ResultSet.NextRow - Start from beginning

h725

Active Member
Licensed User
Longtime User
Good evening,

I am a little bit confused with the ResultSet.NextRow method.
I am converting an APP to B4J. I have a Buttonarray on the one
hand, and the result of a databasequery on the other. In the
result I have the information which database entry fits to
which button. Working B4A Example (shortened):
Example1:
Sub LoadButtons
For x = 0 To (maxX * maxY) - 1
Dim colx As Int =  Round2(((b.left / (pnl.Width / maxX)) + 1),0)
Dim rowy As Int =  Round2(((b.top / (pnl.Height / maxY)) + 1),0)

For i = 0 To cursor.RowCount - 1
   If colx = cursor.GetInt("colx") And rowy = cursorproducts.GetInt("rowy") Then
      b.tag = cursor.Getint("id")
      b.text = cursor.Getstring("name")
   End If

Next
End Sub

Now in B4J you do not have a cursor, only the resultset. If I try something like this:

Example2:
Sub LoadButtons

For x = 0 To (maxX * maxY) - 1

Dim colx As Int =  Round2(((b.left / (pnl.Width / maxX)) + 1),0)

Dim rowy As Int =  Round2(((b.top / (pnl.Height / maxY)) + 1),0)

Do While resultset.nextrow
   If colx = resultset.GetInt("colx") And rowy = resultset.GetInt("rowy") Then
                b.Tag = resultset.GetInt("id")
                b.Text = resultset.GetString("name")
   End If
Loop

Next

The resultset loops through ones for x = 0 . And thats it.

Regards
h725
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
I may be reading it wrong - but you compare to colx and rowy - but they never get updated.
Would you not just get the one result that matches the col/row ?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think that the question is:

using Cursors I can read all records twice or more, inside a for next loop; how to set the RecordSet internal pointer to the first record, so that I can re-read the records from the beginning multiple times?

My answer is: you can't.

You have two options, I think:

a - read all records only once, before and outside the [for next loop], store the records in a data structure (Map, multidimensional array, List of custom types, Map of custom types) and use this structure in your [for next loop];

b - you have to run the query more than once, inside the [for next loop] (closing the ResultSet each time).
 
Upvote 0

h725

Active Member
Licensed User
Longtime User
As always: Thank you very much!
@Daestrum I shortened the code. The part where the internal b (Button) changes was not added. That was probably a little bit confusing.

I made a workaround like LucaMs recommended with a map and a type like this:


B4X:
Sub Process_Globals
Dim cursor as ResultSet
Type btninfo(id As Int,colx As Int,rowy As Int,name As String)
Private btnlist As List
End Sub

Sub GetData
cursor = SQL.ExecQuery2("",...)
   
    Do While cursor.NextRow
        Dim btn As btninfo
        btn.Initialize
        btn.id = cursor.GetInt("id")
        btn.colx = cursor.GetInt("colx")    
        btn.rowy = cursor.GetInt("rowy")
        btn.name = cursor.GetString("name")
        btnlist.Add(btn)
    Loop

End Sub

Sub LoadButtons

For x = 0 To (maxX * maxY) - 1

Dim colx As Int =  Round2(((b.left / (pnl.Width / maxX)) + 1),0)
Dim rowy As Int =  Round2(((b.top / (pnl.Height / maxY)) + 1),0)

For i  = 0 To btnlist.Size -1
            Dim btn As btninfo = btnlist.Get(i)
            If colx = btn.colx And rowy =btn.rowy  Then
                b.Tag = btn.id
                b.Text= btn.name
            End If
Next


End Sub

I will also try Erels solution!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…