备份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(,我想将数据还原到应用程序,我尝试了此代码,但它不是还原数据到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();
}
}

我的问题是如何将SQLite数据库还原到我的应用?

这是工作db还原的核心代码(来自 if (dbfile ..在 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();

主要区别是我删除了数据库文件并使用写入而不是转移。后来和成功还原后,我还使用以下来重新启动该应用程序(可能是过高的功能,但对我有用(,因为您可以获得不可预测的结果(我认为可以从内存/缓存数据访问原始数据库的某些部分(: -

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

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

最新更新