iOS Question ExecQuery error: out of memory

Discussion in 'iOS Questions' started by Shay, Feb 12, 2015.

  1. Shay

    Shay Well-Known Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the full code that causes this crash?
     
  3. Shay

    Shay Well-Known Member Licensed 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


    Code:
    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
    Code:
    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
     
  4. Filippo

    Filippo Expert Licensed User

    Hi,

    I have the same problem with this line:
    Code:
    Private Sub getCursor(Key As StringAs ResultSet
        
    Return sql1.ExecQuery2("SELECT value FROM main WHERE key = ?"Array As String(Key))
    End Sub
    And here is the full code:
    Code:
    '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 StringAs 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 StringAs 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 StringAs 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 StringAs 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 StringAs 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
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload a project that demonstrates this issue (File - Export as zip)?
     
  6. Filippo

    Filippo Expert Licensed User

    Hi Erel,

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

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    It is a mistake to close the SQL connection in this event. Especially as you are not opening it in the Active event.

    You do not need to close it at all. It will be closed when the process is killed.
     
  8. Shay

    Shay Well-Known Member Licensed User

    Thanks, it fixed my issue (not closing sql)
     
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