备份Android后如何恢复Sqlite数据库

时间:2017-06-01 21:42:39

标签: android database sqlite android-sqlite

我搜索了很多关于备份/恢复的Sqlite数据库我发现代码将sqlite文件复制到SD卡这是代码

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();

}
}

现在我在SD卡中有数据库文件(.db),我想将数据恢复到应用程序我尝试了这段代码,但它没有将数据恢复到应用程序

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();

}
}

我的问题是如何将sqlite数据库恢复到我的应用程序?

1 个答案:

答案 0 :(得分:0)

这是工作数据库还原的核心代码(来自if (dbfile <{1>} 中的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();

主要区别在于我删除了DB文件并使用写入而不是传输。稍后,在成功恢复后,我还使用以下内容重新启动应用程序(可能有点矫枉过正,但它对我有用),因为您可能会得到不可预测的结果(我认为可以从内存/缓存数据访问原始数据库的某些部分): -

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


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