user3318703
user3318703

Reputation: 123

When create backup of sqlite database created old file in android pie?

I have code for backup create of local sqlite file. It's work fine lower API 28 but above >= 28 it's create issue to create backup file. When I create backup file that time it's create old data file not updated data but display updated data in application.

public void writeBackupOnSD() {
    try {
        File newFolder = new File(Environment.getExternalStorageDirectory(), "Account_Manager");
        if (!newFolder.exists()) {
            boolean isCreated = newFolder.mkdir();
            Log.d("File", "status" + isCreated);
        }
        File file = new File(newFolder, backupFileName);//custom file name save on sd


        String outFileName = file.getPath();
        String inputFileName = getApplicationContext().getFilesDir().getPath()+ "/" + AppConstant.DATABASE_NAME;

        if(checkDatabase(inputFileName)) {
            FileInputStream myInput = new FileInputStream(new File(inputFileName));
            FileOutputStream myOutput = new FileOutputStream(outFileName);

            FileChannel src = myInput.getChannel();
            FileChannel dst = myOutput.getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            showMessage("Backup successfully!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        showMessage("Failed to backup!");
    }
}

Upvotes: 0

Views: 656

Answers (1)

MikeT
MikeT

Reputation: 56958

With Android Pie and above, the default logging mode has been changed from journal mode to WAL (write ahead logging). With journal mode the transactions are committed and written to the db file on disk and a record of them is kept in the journal allowing roll back of the transactions.

With WAL the transactions are written to a separate file (the db file suffixed with -wal), the wal file is only occasionally written to disk (checkpointed) but is available as if it’s part of the database file. Roll back isn’t needed as basically the –wal file can be dropped.

If you don’t CHECKPOINT the database (closing the database should checkpoint it) and you don’t backup (and restore) an un-checkpointed database along with the –wal file (and properly also the –shm file) then you lose the transactions and basically there may will likely be issues when attempting to open the database. The SQLitedatabase open methods then try to provide a usable database, which is empty.

There are various fixes:-

Force the use of journal mode using disableWriteAheadLogging (not recommended as you lose the advantages offered by WAL).

Backup and restore the -wal and -shm files along with the database file.

Fully checkpoint the database before backing it up and therefore there is no need to backup the -wal and -shm files. - Note closing the database should fully checkpoint the database. - The answer here shows how you can programatically checkpoint Backup sqlite db in WAL mode without FileStreams // by export/backup sql commands

Upvotes: 2

Related Questions