iOS Question ExecQuery error: out of memory

Shay

Well-Known Member
Licensed User
Longtime User
Found strange thing
I am using SQL1 as sql

Cursor1 = SQL1.ExecQuery("SELECT * FROM Favorite")
and at the end I am closing SQL1.close and Cursor1.close

if I use SQL1 again on different place in the module (can be same as above query) it crash with out of memory
to fix it I had to use SQL2
 

Shay

Well-Known Member
Licensed User
Longtime User
first sub load and display the table into customlistview
it crash on the sub click
I changed it to sql2 and it is working fine


B4X:
Sub LoadFromDB

    Dim Cursor1 As ResultSet
    Dim CustomerName, CustomerName2, CustomerPhone, City, Street, Number As String
   
    If SQL1.IsInitialized = False Then
     SQL1.Initialize(File.DirDocuments,"My.db", False)
    End If
   
    Dim sb As StringBuilder
    Dim i As Int = 0
   
    Cursor1 = SQL1.ExecQuery("SELECT * FROM Favorite")
    Do While Cursor1.NextRow
     i=i+1
     sb.Initialize
     CustomerName = (Cursor1.GetString("CustomerName"))
     CustomerName2 = (Cursor1.GetString("CustomerName2"))
     CustomerPhone = (Cursor1.GetString("CustomerPhone"))
     City = (Cursor1.GetString("City"))
     Street = (Cursor1.GetString("Street"))
     Number = (Cursor1.GetString("Number"))
     sb.Append("  " & CustomerName & "  " & CustomerName2)     
     sb.Append(CRLF)
     sb.Append(" " & Street & " " & Number & " " & City)
     ListView1.AddTextItem(sb.ToString,CustomerPhone)
    Loop
   
    If i=0 Then
     Label1.Text = "no data"
    End If
   
    Cursor1.Close
    SQL1.Close
   
End Sub

B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)

    Dim Cursor1 As ResultSet
    If SQL1.IsInitialized = False Then
     SQL1.Initialize(File.DirDocuments,"My.db", False)
    End If
' *** ON NEXT LINE IT CRASH WITH OUT OF MEM  ***
    Cursor1 = SQL1.ExecQuery("SELECT * FROM Favorite WHERE CustomerPhone='" & Value & "'")
    Main.CustomerName = (Cursor1.GetString("CustomerName"))
   
    Cursor1.Close
    SQL1.Close
   
    PG.RootPanel.RemoveAllViews
    Dim step4 As Step4c
    step4.Initialize
   
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Hi,

I have the same problem with this line:
B4X:
Private Sub getCursor(Key As String) As ResultSet
    Return sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
End Sub

And here is the full code:
B4X:
'Class module
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 sql1.IsInitialized Then sql1.Close
    sql1.Initialize(File.DirLibrary, "pm.dat", True)
    CreateTable
End Sub

Public Sub SetBoolean(key As String, value As Boolean)
    PutSimple(key, value)
End Sub

Public Sub GetBoolean(key As String) As Boolean
    Return GetSimpleBoolean(key)
End Sub

Public Sub SetString(key As String, value As String)
    PutSimple(key, value)
End Sub

Public Sub GetString(key As String) As String
    Return GetSimpleString(key)
End Sub

Public Sub GetAll As Map
    Dim c As ResultSet= sql1.ExecQuery("SELECT * FROM main")
    Dim res As Map
    res.Initialize
    Do While c.NextRow   
        res.put(c.GetString2(0),c.GetString2(1))
    Loop
    Return res
End Sub

'creates the main table (if it does not exist)
Private Sub CreateTable
    sql1.ExecNonQuery("CREATE TABLE IF NOT EXISTS main(key TEXT PRIMARY KEY, value NONE)")
End Sub

Private Sub getCursor(Key As String) As ResultSet
    Return sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
End Sub

'Returns a "simple" value. See PutSimple.
Private Sub GetSimpleString(Key As String) As String
    Dim c As ResultSet = getCursor(Key)
    If c.NextRow = False Then
        c.Close
        Return ""
    End If
    Dim res As String = c.GetString2(0)
'    Log("res=" & res)
    c.Close
    Return res
End Sub

'Returns a "simple" value. See PutSimple.
Private Sub GetSimpleBoolean(Key As String) As Boolean
    Dim c As ResultSet = getCursor(Key)
    If c.NextRow = False Then
        c.Close
        Return ""
    End If
    Dim res As String = c.GetString2(0)
    c.Close
    If res = 1 Then
        Return True
    Else
        Return False
    End If
End Sub

'Puts a simple value in the store.
'Strings and number types are considered "simple" values.
Private Sub PutSimple(Key As String, Value As Object) As Boolean
    Try
        start(Key)
        insertQuery(Key, Value)
        Return complete(True)
    Catch
        Return complete(False)
    End Try
End Sub

Private Sub start (Key As String)
    sql1.BeginTransaction
    sql1.ExecNonQuery2("DELETE FROM main WHERE key = ?", Array As Object(Key))
End Sub

Private Sub insertQuery(Key As String, Value As Object)
    sql1.ExecNonQuery2("INSERT INTO main VALUES(?, ?)", Array As Object(Key, Value))
End Sub

Private Sub complete(success As Boolean) As Boolean
    If success Then
        sql1.TransactionSuccessful
    Else
        Log("Error saving object: " & LastException)
        sql1.Rollback
    End If
    Return success
End Sub

'Closes the store.
Public Sub Close
    sql1.Close
End Sub
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

here is my project.
I think the error is in:
B4X:
Private Sub Application_Inactive
'    Log("Application_Inactive")
    'Preferenmanager schliessen
    If manager.IsInitialized Then manager.Close   
End Sub
 

Attachments

  • TestSQL.zip
    4.2 KB · Views: 299
Upvote 0
Top