B4J Question Cursor to Resultset

peacemaker

Expert
Licensed User
Longtime User
HI, All

I have an old B4A big project with lots of loops using Cursors from SQLite db.
All loops are rather complex, big, like:
B4X:
Dim c As Cursor = Starter.SQL.ExecQuery("SELECT * FROM .....")

If c.RowCount = 0 Then  'sometimes such condition is the must
        Return
End If

For i = 0 To c.RowCount - 1
    c.Position = i
    Dim var001 As String = c.GetString("var001")
    .....
    'lots of codes with many VARxxx
Next
'sometimes some extra code here after the loop, so pre-checking RowCount sometimes is strongly required

Now suddently i have to make porting of that old project to B4J.
How to migrate to ResultSet simpler and errorless ?
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
no
B4X:
Dim c As ResultSet = Starter.SQL.ExecQuery("SELECT * FROM .....")

Do While c.NextRow
     Dim var001 As String = c.GetString("var001")
    .....
    'lots of codes with many VARxxx
Loop
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Dim c As ResultSet = Starter.SQL.ExecQuery("SELECT * FROM .....")
Dim RowCount As Int = 0

Do While c.NextRow
     Dim var001 As String = c.GetString("var001")
    .....
    'lots of codes with many VARxxx
    RowCount = RowCount +1
Loop

If RowCount = 0 Then Return
'sometimes some extra code here after the loop, so pre-checking RowCount sometimes is strongly required
 
Last edited:
Upvote 1

aeric

Expert
Licensed User
Longtime User
B4X:
Dim strValue01 As String
Dim intValue02 As Int
Dim dblValue03 As Double
Dim RowCount As Int
Dim Query As String = $"SELECT
var001,
var002,
var003
FROM Table01"$
Dim RS1 As ResultSet = Starter.SQL.ExecQuery(Query)
Do While RS1.NextRow
    strValue01 = RS1.GetString("var001")
    intValue02 = RS1.GetInt("var002")
    dblValue03 = RS1.GetDouble("var003")
    RowCount = RowCount + 1
Loop
RS1.Close

If RowCount = 0 Then Return
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
NextRow you meant, thanks.


Is it not strongly required to close a cursor in B4A ? Maybe due to garbage collector...
But here with ResultSet is always required ?
Yes NextRow
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But how to get the latest row of the ResultSet ? Or any middle position row ?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
But how to get the latest row of the ResultSet ? Or any middle position row ?
It is not possible in B4J. You have to scroll all over again.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
And no any other possibility to work with a db recordset natively full-functional ? Without crossplatform limitations.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
And no any other possibility to work with a db recordset natively full-functional ? Without crossplatform limitations.
Only B4A you have the option to scroll to the first and last. I don't know if there are other ResultSet implementations that have multiple methods.

I usually index records with a unique identifier and recall the specific ROW with a SELECT command. I don't know if there are other ways
 
Upvote 0
Top