Hi LucaMs,
Thanks...I will try it and revert back if any issues...
Hi,
I have done the following changes and the application is working fine.
I have a few more queries. Requesting for guidance.
1) My Service Module Starter.bas has the following code to
connect to the database.
Code in Service Module Starter.bas
----------------------------------
Sub Process_Globals() Public SQL As SQL Public SafeDirectory As String Public RTP As RuntimePermissions Public DBName As String="myDatabaseName.db" End Sub
Sub Service_Create
SafeDirectory = RTP.GetSafeDirDefaultExternal("")
If File.Exists(SafeDirectory, DBName) = False Then
Wait For (File.CopyAsync(File.DirAssets, DBName, SafeDirectory, DBName)) Complete (Success As Boolean) 'File.DirInternal
' ToastMessageShow($"DB Copied = ${Success}"$, False)
Log("Success: " & Success)
End If
ConnectToDatabase
Wait For SQLite_Ready (Success As Boolean)
End Sub
Public Sub ConnectToDatabase
SQL.Initialize( SafeDirectory, DBName, False)
End Sub
2) I created a new class file called clsGEN and wrote the following code to
Code in clsGen class file:
-------------------------
Sub Class_Globals
'**********Declarations for the clsGen ************
Public selectedParamValue As String
End Sub
Public Sub isMyConditionMet() As ResumableSub
'Invoked for clsGen..
'This function returns true if the SQL Condition is satisfied, false otherwise..
Dim SenderFilter As Object
Dim retValue As Boolean
retValue=False
SenderFilter=Starter.SQL.ExecQueryAsync( "SQL",<select count(fieldname) as countOfRecordsFound from tableName where fieldName=selectedParamValue>,Null)
Wait For (SenderFilter) SQL_QueryComplete (Success As Boolean, RS As ResultSet)
If Success Then
RS.NextRow 'must be atleast 1 record
If RS.GetInt("countOfRecordsFound")> 0 Then
retValue=True
Else
retValue=False
End If
RS.Close
Return(retValue)
Else
Log(LastException)
Return(retValue)
End If
End Sub
3) Finally, in my B4XPage, I have:
Code in my B4XMainPage
----------------------
Sub Class_Globals
Private myClass As clsGen
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
myClass.Initialize
End Sub
Sub myCallingSubroutine()
'Passing the parameter to the variable declared in the class file
myClass.selectedParamValue="someValue"
Dim theFunction As ResumableSub=myClass.isMyConditionMet()
Log("checking for the condition)
Wait For(theFunction) Complete (myRetValue As Boolean)
If myRetValue==True Then
'The match is found
Wait For (ShowToast("conditon matched", 2000,"Normal")) Complete(Unused As Boolean)
Else
'The match is not found
Wait For (ShowToast("Condition did not match...", 2000,"ERROR")) Complete(Unused As Boolean)
End If
End Sub
'Note: ShowToast is my User-Defined-function that just displays a coloured message depending on the parameter string I pass to it viz. Normal or ERROR etc..
4) Things that I would like to clarify:
4.1) Whether I have declared and initialized my class module the right way in Point 3)
4.2) Is there a better way to pass the parameter in Point 3 ) than the present way of
myClass.selectedParamValue="someValue"
4.3) The Service Module Starter.bas contains the code for handling the database.
Also please note that I have closed my recordset in my class file in Point 2
but not the database.
In this regard, please refer this post
https://www.b4x.com/android/forum/threads/sql-close.109065/
Here, to a Comment:'Closing the db intentionally is a good practice'
Erel replies as follows:
"Erel: I consider it a bad suggestion.Android apps are different than desktop apps.
They don't have a clear or well defined closing event.
You can make your app more complex than it should be by closing the database
in Activity_Pause (of each activity) and then open it again.
Nothing good will come out of this."
Further, to a query in the same discussion,
Q: 'Leave SQL "open" and wait for OS to close it?'
"Erel:Yes. Unless your app has an explicit exit point. Most apps don't."
Hence, I am unsure of how I should handle proper closure of the database in B4a.
I request you to throw more light on it.
I personally, am from an environment where we keep db connections open for minimal
amount of time - opening just before invoking a function and immediately
close the connection. Honestly, it gives me the creeps to leave database connections unattended.
5) Lastly, in the above referred post
Erel seems to mean that
if your application has an explicit exit point you can close
the connection.
In view of the above, assuming that my B4XMainPage is the exit point of my application,
how should database closure be properly implemented in my B4xMainPage ?
Thanks.