Héctor
Héctor

Reputation: 26054

Refresh SQLite database reference after restore it

I have a backups module in my that restores the SQLite database from a backup. First, it removes the current database file and then copy the backup database bytes to the database directory. Once database is restored, I ask for the user to restart the app, including remove it from memory. This is working properly.

Is there a way to "refresh" the reference to the database file programatically? In this way, user has not to close the app.

My database is stored in /data/data/com.mypackage.project/databases/mydatabase.db and the backups are in sdcard.

I restore the database file like this:

public static boolean transferBytes(File from, File to) {
        FileChannel inputChannel = getInputStream(from).getChannel();
        FileChannel outputChannel = getOutputStream(to).getChannel();
        return transfer(inputChannel, outputChannel);
    }

private static boolean transfer(FileChannel inputChannel, FileChannel outputChannel) {
        try {
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            inputChannel.close();
            outputChannel.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

Where from is the backup file and to a file instance with the database path mentioned above.

Upvotes: 3

Views: 178

Answers (1)

VCHe
VCHe

Reputation: 32

Did you try to create a new instance of DBHelper after restoring db?

Upvotes: 1

Related Questions