Android Question ResultSet RowCount after Close

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Noticed that after closing a ResultSet (closing the ResultSet and freeing resources) the RowCount remains as before closing the ResultSet. As I have a number of module level ResultSets that remain open I added a Sub to keep track of these and close all open ResultSets if there was a memory problem (although this is very unlikely). To check the real rows I added a little function:

B4X:
Sub GetRSRealRows(RS1 As ResultSet) As Long
 Try
  RS1.Position = -1
  RS1.NextRow
 Catch
  Return 0
 End Try
 Return RS1.RowCount
End Sub

Is there any way to get the real RowCount without a Try Catch?

RBS
 

MarkusR

Well-Known Member
Licensed User
Longtime User
after close does this work?
B4X:
RS1 = null
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Noticed that after closing a ResultSet (closing the ResultSet and freeing resources) the RowCount remains as before closing the ResultSet. As I have a number of module level ResultSets that remain open I added a Sub to keep track of these and close all open ResultSets if there was a memory problem (although this is very unlikely). To check the real rows I added a little function:

B4X:
Sub GetRSRealRows(RS1 As ResultSet) As Long
 Try
  RS1.Position = -1
  RS1.NextRow
 Catch
  Return 0
 End Try
 Return RS1.RowCount
End Sub

Is there any way to get the real RowCount without a Try Catch?

RBS

For completeness, function should be like this:

B4X:
Sub GetRSRealRows(RS1 As ResultSet) As Long
 Dim lPosition As Long
 Try
  lPosition = RS1.Position
  RS1.Position = -1
  RS1.NextRow
 Catch
  Return 0
 End Try
 RS1.Position = lPosition 'we shouldn't alter the position!
 Return RS1.RowCount
End Sub

As the old position in the ResultSet may be needed.

RBS
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
emm, your purpose is to have/use the row count after close the result set?
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
emm, your purpose is to have/use the row count after close the result set?

It is part of a Sub to see the state of various ResultSets and close all if needed.
It is more of a dev thing than a normal user thing.

This is the full code:

B4X:
Sub GetRSRealRows(RS1 As ResultSet) As Long
 
 Dim lPosition As Long
 
 Try
  lPosition = RS1.Position
  RS1.Position = -1
  RS1.NextRow
 Catch
  Return -1 'so we can differentiate between closed (or uninitialized) RS and empty one
 End Try
 
 RS1.Position = lPosition 'we shouldn't alter the position!
 Return RS1.RowCount
 
End Sub

Sub ClearResultSets
 
 Dim i As Int
 Dim iCount As Int
 Dim iRSRows As Int
 Dim iRSRowsTotal As Int
 Dim strResult As String
 Dim arrClose(arrResultSets.Length) As Boolean
 
 For i = 0 To arrResultSets.Length - 1
  If arrResultSets(i).IsInitialized Then
   iRSRows = GetRSRealRows(arrResultSets(i))
   If iRSRows > -1 Then
    iCount = iCount + 1
    iRSRowsTotal = iRSRowsTotal + iRSRows
    arrClose(i) = True
    If iCount = 1 Then
     strResult = arrResultSetNames(i) & " - " & iRSRows
    Else
     strResult = strResult & CRLF & arrResultSetNames(i) & " - " & iRSRows
    End If
   End If
  End If
 Next
 
 If iCount = 0 Then
  Dim rs As ResumableSub = Dialog.Show(Activity, Array As Object("OK"), _
           "Close ResultSets", "", "No open ResultSets", _
           -1, False, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, arrS)
  Wait For (rs) Complete (strResult As String)
  Return
 End If
 
 'Chr(45) = -
 strResult = strResult & CRLF & General.MakeString(45, 56) & CRLF & "Total - " & iRSRowsTotal
 
 Dim rs As ResumableSub = Dialog.Show(Activity, Array As Object("OK", "Close All"), _
          "Close ResultSets (" & iCount & ")", "", strResult, _
          -1, False, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, arrS)
 Wait For (rs) Complete (strResult As String)
 
 If strResult = "Close All" Then
  For i = 0 To arrResultSets.Length - 1
   If arrClose(i) Then
    arrResultSets(i).Close
   End If
  Next
 End If
 
End Sub

Sub MakeString(btByte As Byte, lNum As Long) As String
 
 Dim i As Long
 Dim arrBytes(lNum) As Byte
 
 For i = 0 To lNum - 1
  arrBytes(i) = btByte
 Next
 
 Return BC.StringFromBytes(arrBytes, "ASCII")

End Sub

I could of course keep track of a closing RecordSets via a Boolean array for example, but this seems simpler.

RBS
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
how about using a class to open/close recordsets, it can collect opened into a list and if you close it via a method it can remove it from list.
a class/object would also make it easier to access data.
additional you can use a type/struct and collect more information.
B4X:
type opened(rs as resultset,usage as string,flag as int,recordcount as int,...)
 
Upvote 0
Top