Android Question check if cursor is closed

Makumbi

Well-Known Member
Licensed User
Please help how can i check to see if the cursor is already closed am getting this error on my app
B4X:
autofield: 1
[{"Account":"05-00001","balance":-866926,"class":"S2","Stream":"A","Names":"BUGEMBE YASIN ismail","autofield":1},{"Account":"05-00005","balance":2010000,"class":"S5","Stream":"A","Names":"DDAMULIRA GEORGE","autofield":5},{"Account":"05-00013","balance":1910000,"class":"S5","Stream":"A","Names":"ASADU","autofield":13}]
Account: 05-00005
Amount: 2010000
Class: S5
Stream: A
Names: DDAMULIRA GEORGE
autofield: 5
[{"Account":"05-00001","balance":-866926,"class":"S2","Stream":"A","Names":"BUGEMBE YASIN ismail","autofield":1},{"Account":"05-00005","balance":2010000,"class":"S5","Stream":"A","Names":"DDAMULIRA GEORGE","autofield":5},{"Account":"05-00013","balance":1910000,"class":"S5","Stream":"A","Names":"ASADU","autofield":13}]
Account: 05-00013
Amount: 1910000
Class: S5
Stream: A
Names: ASADU
autofield: 13
Error occurred on line: 235 (Main)
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/kccug.com/files/studentdbmk.sql
    at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
    at anywheresoftware.b4a.sql.SQL.ExecQuery2(SQL.java:223)
    at anywheresoftware.b4a.sql.SQL.ExecQuery(SQL.java:211)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)

B4X:
Sub ExecuteHtml(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, Clickable As Boolean) As String
    'adjust by sula
    Private cur As Cursor
    
    If StringArgs <> Null Then
        cur = Starter.SQL1.ExecQuery2(Query, StringArgs)
    Else
        
        cur = Starter.SQL1.ExecQuery(Query)
    End If
    Log("ExecuteHtml: " & Query)
    If Limit > 0 Then Limit = Min(Limit, cur.RowCount) Else Limit = cur.RowCount
    Private sb As StringBuilder
    sb.Initialize
    sb.Append("<html><body>").Append(CRLF)
    sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
    sb.Append("<table><tr>").Append(CRLF)
    For i = 0 To cur.ColumnCount - 1
        sb.Append("<th>").Append(cur.GetColumnName(i)).Append("</th>")
    Next
    
    sb.Append("</tr>").Append(CRLF)
    For row = 0 To Limit - 1
        cur.Position = row
        If row Mod 2 = 0 Then
            sb.Append("<tr>")
        Else
            sb.Append("<tr class='odd'>")
        End If
        For i = 0 To cur.ColumnCount - 1
            sb.Append("<td>")
            If Clickable Then
                sb.Append("<a href='http://").Append(i).Append(".")
                sb.Append(row)
'                sb.Append(".com'>").Append(cur.GetString2(i)).Append("</a>")
                sb.Append(".stub'>").Append(cur.GetString2(i)).Append("</a>")
        
            Else
                sb.Append(cur.GetString2(i))
            End If
            sb.Append("</td>")
        Next
        sb.Append("</tr>").Append(CRLF)
        
    Next
cur.Close
    sb.Append("</table></body></html>")
    Return sb.ToString
End Sub
 

MarkusR

Well-Known Member
Licensed User
Longtime User
i believe its because this database is closed?
Starter.SQL1
can't see what is "on line: 235 (Main)"
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
i believe its because this database is closed?

can't see what is "on line: 235 (Main)"
This is what at line 235 the one that is bold
B4X:
If StringArgs <> Null Then
        cur = Starter.SQL1.ExecQuery2(Query, StringArgs)
    Else
        
        cur = Starter.SQL1.ExecQuery(Query)
    End If
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i suggest making a open and close sub then you can add a Log("...") and see when its open and close.
i think you use this SQL1 object here direct.
example:
B4X:
Public Sub Open() As SQL

    Log("Open Database")
    SQL1.Initialize(Path, FileName, False)
   
    Return SQL1
   
End Sub

Public Sub Close
   
    Log("Close Database")

    SQL1.Close
   
End Sub

my db class
B4X:
'...
Sub Class_Globals
    Private SQL1 As SQL
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
    #IF B4A   
    If File.Exists(Path,FileName)= False Then
        File.Copy(File.DirAssets,"Template.db",Path,FileName)
        ToastMessageShow("Copied new Database from Template",True)
    End If
    #End If
    
End Sub

Public Sub Open() As SQL

    Log(Path)
    SQL1.Initialize(Path, FileName, False)
    
    Return SQL1
    
End Sub

Public Sub Close
    
    SQL1.Close
    
End Sub

Sub Path As String
    
    Dim rtp As RuntimePermissions
    
    Return rtp.GetSafeDirDefaultExternal("db")
    
End Sub

Sub FileName As String
        
    Return "Inspections.db"
    
End Sub
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
i suggest making a open and close sub then you can add a Log("...") and see when its open and close.
i think you use this SQL1 object here direct.
example:
B4X:
Public Sub Open() As SQL

    Log("Open Database")
    SQL1.Initialize(Path, FileName, False)
  
    Return SQL1
  
End Sub

Public Sub Close
  
    Log("Close Database")

    SQL1.Close
  
End Sub

my db class
B4X:
'...
Sub Class_Globals
    Private SQL1 As SQL
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   
    #IF B4A  
    If File.Exists(Path,FileName)= False Then
        File.Copy(File.DirAssets,"Template.db",Path,FileName)
        ToastMessageShow("Copied new Database from Template",True)
    End If
    #End If
   
End Sub

Public Sub Open() As SQL

    Log(Path)
    SQL1.Initialize(Path, FileName, False)
   
    Return SQL1
   
End Sub

Public Sub Close
   
    SQL1.Close
   
End Sub

Sub Path As String
   
    Dim rtp As RuntimePermissions
   
    Return rtp.GetSafeDirDefaultExternal("db")
   
End Sub

Sub FileName As String
       
    Return "Inspections.db"
   
End Sub


The error has persisted i have attached the file please help
 

Attachments

  • ShipError.zip
    222.2 KB · Views: 262
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
its closed here, i used find and search for "SQL1.Close"
Main
Sub DATA

and created here
Starter
Service_Create

try to avoid global variables
Main
Process_Globals
Dim cursor1 As Cursor

i can't test your project because i not have all librarys.
 
Last edited:
Upvote 0
Top