Android Question ResultSet RowCount after Close

Discussion in 'Android Questions' started by RB Smissaert, Apr 22, 2019.

  1. RB Smissaert

    RB Smissaert Well-Known Member Licensed 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:

    Code:
    Sub GetRSRealRows(RS1 As ResultSetAs 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
     
  2. MarkusR

    MarkusR Well-Known Member Licensed User

    after close does this work?
    Code:
    RS1 = null
     
  3. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    No, that will produce False after closing the RS.

    RBS
     
  4. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    For completeness, function should be like this:

    Code:
    Sub GetRSRealRows(RS1 As ResultSetAs 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
     
  5. MarkusR

    MarkusR Well-Known Member Licensed User

    emm, your purpose is to have/use the row count after close the result set?
     
  6. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    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:

    Code:
    Sub GetRSRealRows(RS1 As ResultSetAs 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(ActivityArray As Object("OK"), _
               
    "Close ResultSets""""No open ResultSets", _
               -
    1False00000000000NullFalse, arrS)
      
    Wait For (rs) Complete (strResult As String)
      
    Return
     
    End If
     
     
    'Chr(45) = -
     strResult = strResult & CRLF & General.MakeString(4556) & CRLF & "Total - " & iRSRowsTotal
     
     
    Dim rs As ResumableSub = Dialog.Show(ActivityArray As Object("OK""Close All"), _
              
    "Close ResultSets (" & iCount & ")""", strResult, _
              -
    1False00000000000NullFalse, 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
     
  7. MarkusR

    MarkusR Well-Known Member Licensed 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.
    Code:
    type opened(rs as resultset,usage as string,flag as int,recordcount as int,...)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice