4

I know about copying the database file solutoin,

How do I backup a database file to the SD card on Android?

Android backup/restore: how to backup an internal database?

Android backup/restore: how to backup an internal database?

in fact I was backing up my databases using this method, and up until a little while ago it was working perfectly. until some multi-thread stuff made me use of enableWriteAheadLogging now I have two more files near the database file with .db-wal and .db-shm extensions.

copying just the datbase file .db is not working as most of the times the file does not contain the latest database commits (which are available throw app itself) however when I copy three files together it seems to work fine (not quit sure, albeit)

as Sqlite people recommend, the best practice for backing up a sqlite database is to use backup api but can someone guid me on how I can use this api from inside an application, or even use sqlite .dump (How do I dump the data of some SQLite3 tables?) method from inside an app ?

So Which one is the best practice to back up a sqlite database from an android app?

1- Copy all the database-related files from the sandbox

2- Use sqlite Backup Api

3- Use sqlite .dump

4- any other method

Community
  • 1
  • 1
Muhammad Naderi
  • 3,090
  • 3
  • 24
  • 37

2 Answers2

1

The SQLite backup API is the only mechanism that works correctly with concurrent write accesses from other threads/processes. Which of course means that the Android database framework does not give you access to it.

If you are sure that there are not any active connections, you can just copy all files. (Leaving out journal or WAL files would lead to data corruption.) In the case of WAL, the -shm file does not contain any permanent data and could be omitted.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

The VACUUM INTO command introduced in SQLite version 3.27.0 (2019-02-07) can serve as an alternative to the backup API.

Mark
  • 7,446
  • 5
  • 55
  • 75