备份Android后如何还原Sqlite数据库 [英] How to restore Sqlite database after backup Android

查看:519
本文介绍了备份Android后如何还原Sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多有关备份/还原Sqlite数据库的信息,我发现了将sqlite文件复制到SD卡的代码,这是代码

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

}
}

现在我在SD卡中有数据库文件(.db),我想将数据还原到应用程序,我尝试了此代码,但它不是将数据还原到应用程序

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

}
}

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

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

推荐答案

这是有效的数据库还原的核心代码(来自 try 中的if (dbfile ..).

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

这篇关于备份Android后如何还原Sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆