iOS Question SQL ExecQuery error: out of memory

susu

Well-Known Member
Licensed User
Hi,
I'm playing with SQL by create a simple app with 2 pages. However, it only works first time, the second time run there's error "SQL ExecQuery error: out of memory" even the database is very small.
Here the code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1, Page2 As Page
   
    Public SQL1 As SQL
    Public SQLDataBasePath = File.DirDocuments As String
    Public SQLDateBaseName = "persons.db" As String
    Public SelectedName As String
    Private btJohn, btGeorge As Button
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    Page1.RootPanel.LoadLayout("page1")
    NavControl.ShowPage(Page1)
   
    Page2.Initialize("Page2")
    Page2.Title = "Page 2"
    Page2.RootPanel.LoadLayout("page2")
   
   
    If File.Exists(SQLDataBasePath, SQLDateBaseName) = False Then
        File.Copy(File.DirAssets, SQLDateBaseName, SQLDataBasePath, SQLDateBaseName)
        SQL1.Initialize(SQLDataBasePath, SQLDateBaseName, False)
    Else
        SQL1.Initialize(SQLDataBasePath, SQLDateBaseName, False)
    End If
End Sub

Sub btJohn_Click
    SelectedName = "John"
    NavControl.ShowPage(Page2)
End Sub

Sub btGeorge_Click
    SelectedName = "George"
    NavControl.ShowPage(Page2)
End Sub

Private Sub page2_Appear
    Dim query As String
    query = "select ID from persons where FirstName = '"&SelectedName&"'"
    Dim rs As ResultSet
    rs = SQL1.ExecQuery(query)
    Do While rs.NextRow
        Log(rs.GetString("ID"))
    Loop
    SQL1.Close
End Sub

Or you can find the attachment. Thanks.
 

Attachments

klaus

Expert
Licensed User
The problem is in Private Sub page2_Appear where you close SQL1 at the end !
You should replace it by rs.Close and move SQL1.Closr to Application_Background.
B4X:
Private Sub page2_Appear
    Dim query As String
    query = "select ID, LastName from persons where FirstName = '"&SelectedName&"'"
    Dim rs As ResultSet
    rs = SQL1.ExecQuery(query)
    Do While rs.NextRow
        Log(rs.GetString("ID") & " : " & rs.GetString("LastName"))
    Loop
    rs.Close
End Sub

Private Sub Application_Background
    SQL1.Close
End Sub
 

susu

Well-Known Member
Licensed User
Klaus, you saved my day!
 

susu

Well-Known Member
Licensed User
move SQL1.Closr to Application_Background
Hi Klaus,

I moved SQL1.Close to Application_Background, it will be crash if I hit Home button then back to the app. I know the reason is SQL1 was closed. So is there any solution to close and re-open SQL?
 
Top