But extra question: if SQL object is declared inside a service, and the service sometimes must be stopped for changing initial important settings and also database re-initing - but an activity should going on working with SQL - how to organize such app ?
If the SQL object is a process global object then it will stay alive as long as the process is running. It doesn't matter whether the service is destroyed and recreated (assuming that you are not using the async features which do require a context).
This works for me throughout the entire application
Create a code Module Call it DbConnect
Add The following code
'Subs in this code module will be accessible from all modules.
Dim dbSQL As SQL
Dim dbCursor As Cursor
'Change to Correct path of your database
dbSQL.Initialize(File.DirDefaultExternal, "yourDB.db", True)
in your Service Module Add the following Code in Service_Create
'You can add this also in activities
If DbConnect.dbSQL.IsInitialized = False Then
'Call your sql from anywhere
DbConnect.dbSQL.ExecQuery("SELECT * From YourDb")
I noticed that SQL database can't be updated by INSERT statement in Service. Read operations work fine. I tried both ExecNonQuery using Transaction, AddNonQueryToBatch/ExecNonQueryBatch. Both return positive result but database is not updated. Tried to use both SQL (initializing the same database as in main activity) and Main.SQL. Nothing helped. Various magic stuff like 'android metadata' didn't help either.
The same code is working flawless in main activity.
If Main.SQL1.IsInitialized Then Main.SQL1.Close
If SQL2.IsInitialized=False Then
SQL2.Initialize(DBFileDir, DBFileName, False)
Cursor=SQL2.ExecQuery("SELECT * FROM Table;")
The last line has Result: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:
This is indeed strange: I closed a database of Main activity and reinitialized the same database in service module.
My bad is that I don't carefully read your theory, Erel:
Rules - If the target module is already running then the sub will be called.
- If the target module is a service and it is not already running then it will first be started (Service_Create and Service_Start will first be executed). The sub will be called after Service_Start.