Android Question SQLite database writes seem cached

Creaky

Member
Licensed User
Longtime User
Hi all,

I'm running into a strange problem with the SQLite database in my app. This only occurs on SDKversion 28 and higher.

My app uses a SQLite database to store astronomical observations. The app has a backup function so that the user can export a backup of the database to external storage for safe keeping or migration to another device. Before I backup the database, I close it and then I copy the database from Internal storage to the external storage. This is the code:

B4X:
Sub makeBackup
    Dim strFileName As String
    DateTime.DateFormat = "yyyy-MM-dd_HH-mm"
    strFileName = "StarLog backup-" & DateTime.Date(DateTime.Now) & ".slbak"
    Database.Close
    Try
        File.Copy(File.DirInternal, "StarLog.db", File.DirRootExternal & "/StarLog", strFileName)
        JVCUtils.toastMessage(Activity,NVPanel,Main.trans.GetText("txtBackupSucces"))
    Catch
        JVCUtils.toastMessage(Activity,NVPanel,Main.trans.GetText("txtErrorMakingBackup"))
    End Try
    Database.Initialize(File.DirInternal, "StarLog.db", False)
End Sub
These actions run fine, I find the backup file in the external storage location.

But.

The backup file does not contain the changes written to the database since the application started!
So the app shows data that is not found in the backup database... The changes only show up in the backup after the app is closed, restarted and a new backup is made.

It looks like the runtime database is not stored in File.DirInternal until the app is closed.
Where do I find the runtime database?
 

kisoft

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This looks like a problem with permissions to write to an external card.
https://www.b4x.com/android/forum/t...ns-android-6-0-permissions.67689/#post-428719.
You have to wait for Erel, this is similar thread when SDKversion =28 there are problems with access to an external card.
I'm not familiar with such issues.

If the database journal mode is 'wal' then the database will be made of three files. It can be set to 'wal' by default.

Check the output of:
B4X:
Log(File.ListFiles(File.DirInternal))
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
I notice two things.

1. When I completely delete my app from my device, then compile and install it through the debugger on my device, list the files in File.DirInternal, I see:

(ArrayList) [Objects.db-shm, Objects.db-wal, StarLog.db-shm, StarLog.db-wal, Objects.db, StarLog.db, virtual_assets]​

That is before I copied the databases from DirAssets to DirInternal! Why are these files still there after I completely uninstalled the app?

2. I already use this code to disable WAL:

Database.Initialize(File.DirInternal, "StarLog.db", False)
If p.SdkVersion >= 26 Then
Dim r As Reflector
r.Target = Database
r.Target = r.GetField("db")
r.RunMethod("disableWriteAheadLogging")
End If

And when I check the database in the debugger I see that WAL is not enabled

p.jpeg


Is disabling WAL a one time thing or do I need to disable it every time I initialise the database?
 

Attachments

  • Annotation 2019-12-06 094543.jpg
    Annotation 2019-12-06 094543.jpg
    45.1 KB · Views: 227
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Sorry for this question, how did you uninstall the application?
Did you only remove the icon from the panel?
Did you uninstall the application from the settings level?
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
I uninstalled on the application level. The DirDefaultExternal folder was deleted for instance.
 
Upvote 0

Creaky

Member
Licensed User
Longtime User
To get around this weird problem I decided to make a dedicated SUB to initialise and open the database and disable WAL. This works as far as I can tell.
For the record; you need to disable WAL EVERY time you initialise a SQL database.
 
Upvote 0
Top