How to restore Sqlite database after backup Android

馋奶兔 提交于 2019-12-12 06:54:26

问题


I searched a lot about backup/restore Sqlite database i found code to copy sqlite file to SD card this is the code

private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();

if (sd.canWrite()) {
    String currentDBPath = "//data//" + "<package name>"
            + "//databases//" + "<db name>";
    String backupDBPath = "<destination>";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);

    FileChannel src = new FileInputStream(currentDB).getChannel();
    FileChannel dst = new FileOutputStream(backupDB).getChannel();
    dst.transferFrom(src, 0, src.size());
    src.close();
    dst.close();
    Toast.makeText(getApplicationContext(), "Backup Successful!",
            Toast.LENGTH_SHORT).show();

}
} catch (Exception e) {

Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
        .show();

}
}

Now i have database file (.db) in SD card and i want to restore data to app i tried this code but it is not restore data to app

private void importDB() {
try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
        String currentDBPath = "//data//" + "<package name>"
                + "//databases//" + "<database name>";
        String backupDBPath = "<backup db filename>"; // From SD directory.
        File backupDB = new File(data, currentDBPath);
        File currentDB = new File(sd, backupDBPath);

    FileChannel src = new FileInputStream(backupDB).getChannel();
    FileChannel dst = new FileOutputStream(currentDB).getChannel();
    dst.transferFrom(src, 0, src.size());
    src.close();
    dst.close();
    Toast.makeText(getApplicationContext(), "Import Successful!",
            Toast.LENGTH_SHORT).show();

}
} catch (Exception e) {

Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
        .show();

}
}

My question is how i can restore sqlite database to my app?


回答1:


This is the core code of a working DB restore (from if (dbfile .. in a try).

            private static final int BUFFERSZ = 32768;
            private byte[] buffer = new byte[BUFFERSZ];
            ........
            dbfile = new File(currentdbfilename);
            .......
            if (dbfile.delete()) {
                origdeleted = true;
            }

            FileInputStream bkp = new FileInputStream(backupfilename);
            OutputStream restore = new FileOutputStream(currentdbfilename);
            copylength = 0;
            while ((copylength = bkp.read(buffer)) > 0) {
                restore.write(buffer, 0, copylength);
            }
            restore.flush();
            restore.close();
            restoredone = true;
            bkp.close();

The main differences are that I delete the DB file and use writes rather than transfers. Later and upon a successful restore I also use the following to restart the App (might be overkill but it works for me) as you can get unpredictable results (I think parts of the original database may be accessed from memory/cached data):-

    Intent i = getBaseContext().getPackageManager()
                                            .getLaunchIntentForPackage( getBaseContext().getPackageName() );


    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    finish();
    startActivity(i);
    System.exit(0);


来源:https://stackoverflow.com/questions/44317525/how-to-restore-sqlite-database-after-backup-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!