Android Question [SOLVED]Error : attemp to re-open an already closed object

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have an barcode app that using sqlite in memory to store temporary data. The declaration in main module
B4X:
Sub Process_Globals
    Public SQLMem As SQL
End Sub

Initialize in Starter service
B4X:
Sub Service_Create
    Main.SQLMem.Initialize("", ":memory:", True)
    Main.SqlMem.ExecNonQuery("CREATE TABLE MEM_BRCD(ID int,BARCODE text,DSCP text,QTY int)")
End Sub

There are 2 module that use this sqlite, Module A & Module B.
Module B, scan the barcode & insert the result in table MEM_BRCD, and after scan finished call module A to display the data.

Sub to insert the scan data to table in Module B
B4X:
Sub BarcodeOk(Id as Int,Barcode As String,Dscp as String)
   Main.SQLMem.ExecNonQuery2("insert into MEM_BRCD values (?,?,?,?)",Array As Object(Id,Barcode,Dscp,1))
End Sub

Sub to call in module B after scan finished
B4X:
Sub ScanFinished
    If SubExists(Caller,"ScanResult") Then
        CallSubDelayed(Caller,"ScanResult")
    End If
    Activity.Finish
End Sub

Sub ScanResult in Module A
B4X:
Sub ScanResult
    Try
        Private Cur As Cursor
        Cur = Main.SQLMem.ExecQuery("select ID,BARCODE,DSCP,sum(QTY) as QTY from MEM_BRCD group by ID,BARCODE,DSCP")
       
        For i = 0 To Cur.RowCount - 1
            Cur.Position = i
            Main.SQLMem.ExecNonQuery2("insert into MEM_M_FNGD_SO values (?,?,?,?,?)",Array As Object(Cur.GetInt("ID"),Cur.GetString("BARCODE"),Cur.GetString("DSCP"),Cur.GetInt("QTY"),0))
            Cur.Close
        Next
        Cur.Close
    Catch
        Log(LastException)
    End Try
End Sub

Here are the strange things :
1) Scan only one Barcode# for ex : 123, Sub ScanResult in module A runs OK
2) Scan more than one Barcode#, for ex : 123 & 234, and error raised at this line
B4X:
Cur = Main.SQLMem.ExecQuery("select ID,BARCODE,DSCP,sum(QTY) as QTY from MEM_BRCD group by ID,BARCODE,DSCP")

Any hints, how to solve this error?

Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
 For i = 0 To Cur.RowCount - 1
            Cur.Position = i
            Main.SQLMem.ExecNonQuery2("insert into MEM_M_FNGD_SO values (?,?,?,?,?)",Array As Object(Cur.GetInt("ID"),Cur.GetString("BARCODE"),Cur.GetString("DSCP"),Cur.GetInt("QTY"),0))
            Cur.Close
        Next

Why are you closing the cursor even if you are still inside the loop???
It crashs on the 2nd iteration i guess
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
You were right !

I guest I need a coffee break, Thanks a lot for your help.

Now it worked OK.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Don't worry, i know what i am doing.

It was just a moment of an err after long working hours.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Like i said before, i was tired.

Afterall you don't know the project i was working on.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…