SQLiteException: Unable to close due to unfinalised statements

cmweb

Active Member
Licensed User
Longtime User
Hi all,

one of the users of my app reports a problem.

The error message is:

"SQLiteException: Unable to close due to unfinalised statements - Continue? Yes/no"

The error appears when running this sub:

B4X:
Sub listfill
   Main.iconlist.initialize
   Main.sql1.Initialize(Main.sddirectory,"copysmiley.db",True)
   Main.sql1.BeginTransaction
   Dim cursor1 As Cursor
   If Main.selectedcat = "" Then cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1")
   If Main.selectedcat = Main.alltext Then cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1")
   If Main.selectedcat <> "" AND Main.selectedcat <> Main.alltext Then
      cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1 WHERE category = '" & Main.selectedcat & "'")
   End If
   i = 0
   y = 10
   For i = cursor1.RowCount - 1 To 0 Step -1
      cursor1.Position = i
      Main.IconList.Add(cursor1.GetString("url"))
      Dim cursor2 As Cursor
      cursor2 = Main.sql1.ExecQuery2("SELECT image FROM table1 WHERE url = ?",Array As String(cursor1.GetString("url")))
      cursor2.Position = 0
      Dim Buffer() As Byte 'declare an empty byte array
         Buffer = cursor2.GetBlob("image")
       Dim InputStream1 As InputStream
       InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
      Dim bd As Bitmap
      bd.Initialize2(InputStream1)
      InputStream1.Close
      s = bd
      imgHeight = 40dip
      imgWidth = 40dip * s.Width / s.height
      If imgWidth > breite Then
         imgWidth = breite - 5
         End If
      If x > breite - imgWidth Then 
         x = 10
         y = y + 60dip
         End If
      Dim imvImage As ImageView
      imvImage.Initialize("imvImage")
      imvImage.Gravity=Gravity.FILL
      imvImage.Tag=cursor1.GetString("url")
      imvImage.Bitmap=s 
      scvImages.Panel.AddView(imvImage,x,y,imgWidth,imgHeight)
      x = x + imgWidth + 30
   Next
   Main.sql1.TransactionSuccessful
   Main.sql1.EndTransaction
   cursor1.Close
   cursor2.close
   Main.sql1.close
   SaveButton.Visible = True
   scvImages.Visible = True
   scvImages.Panel.Color = Colors.Yellow
   scvImages.Panel.Height = y + 200
   ToastMessageShow(scvImages.Panel.NumberOfViews & " Smileys " & Main.loadedtext,False)
End Sub

I don't have any idea, why this happens.

Anyone any idea to lead me the way?

Best regards,

Carsten
 

poseidon

Member
Licensed User
Longtime User
firstly do these lines as :
B4X:
If Main.selectedcat = "" Then 
      cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1")
else If Main.selectedcat = Main.alltext Then 
      cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1")
else  
      cursor1 = Main.sql1.ExecQuery("SELECT url FROM table1 WHERE category = '" & Main.selectedcat & "'")
End If

then at end :
B4X:
    'first close all
    cursor1.Close
    cursor2.close
    Main.sql1.close

    'then end transcation
    Main.sql1.TransactionSuccessful
    Main.sql1.EndTransaction
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Thanks,

I'm getting a

java.lang.IllegalStateException: database /mnt/sdcard/copysmiley/copysmiley.db (conn# 0) already closed

then in line Main.sql1.close

!?

Best regards,

Carsten
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Unfortunately, the specific device of that user still forces that error message.

When I remove the Main.sql1.close statement, then it works for that user.

But I don't think it is a good idea to not close the database...!?

Any further ideas?

Best regards,

Carsten
 
Upvote 0

poseidon

Member
Licensed User
Longtime User
re

no cant be!
for sure user didnt run the updated apk... for example make a difference on GUI to see if user can see it(?)

also if you call the listfill more than 1time, dont open/close the dbase on this procedure just declare the sql1 on code module (aka modulename.sql1) and use it from there.
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
User installed and tried the updated apk. Definetely.

When I remove main.sql1.close it works. With that line, Sqliteexception appears. Tried it several times now with that user.

The app is running on more than 2000 devices now, but I never heard of that error. Only this one user got this problem...

Strange...

Best regards,

Carsten
 
Upvote 0

poseidon

Member
Licensed User
Longtime User
re

at user the IconList filled ? or is empty?

[edit]
better do :

B4X:
    'first close all
cursor1.Close
cursor2.close

'then end transcation
Main.sql1.TransactionSuccessful
Main.sql1.EndTransaction

if Main.sql1.IsInitialized  then Main.sql1.close

and you will be ok!
 
Last edited:
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
With main.sql1.close.isinitialized... Statement, the Sqliteexception appears again.

Yes, there is definetely data in IconList...
(if there would be no data, it would be impossible to get to sub listfill)

Best regards,

Carsten
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
It is impossible to help without seeing the code.

Hi Erel,

Do you have further ideas why that happens with my code? And only on one device?

What happens, when I don't close main.sql1?
Or could I do that main.sql1.close with "try"?

Best regards,

Carsten
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
You Dim cursor2 in the For / Next loop Dim cursor2 As Cursor, but you close it outsides.
Try to put the line cursor2.Close insides the For / Next loop.

Best regards.
That sounds like a good idea...

Thanks, Klaus, I'll give it a try.

Best regards,

Carsten
 
Upvote 0
Top