简体   繁体   English

Android备份和还原SD卡的数据库

[英]Android backup and restore database to and from SD-card

I'm currently looking to build a backup feature in my Android application. 我目前正在寻找在我的Android应用程序中构建备份功能。 However I'm kinda struggling before even starting to implement it because I'm not sure what the correct way to go is. 然而,在开始实施它之前,我有点挣扎,因为我不确定正确的方法是什么。

I found some interesting articles on the net and so I came up with three possible solutions: 我在网上发现了一些有趣的文章,所以我提出了三种可能的解决方案:

  1. Backup the entire DB file to the SD card 将整个DB文件备份到SD卡
  2. Export the DB-data to an XML file on the SD card 将DB数据导出到SD卡上的XML文件
  3. Use the Android backup mechanism to backup the entire DB to the Google cloud 使用Android备份机制将整个数据库备份到Google云

Now I was wondering what you guys think about these 3 solutions, or do you know another (maybe an even better way to go) and what is in your eyes the best way to go? 现在我想知道你们对这三种解决方案的想法,或者你知道另一种解决方案(也许是一种更好的方法)以及你眼中最好的方法是什么?

Here are my remarks on the possible implementations: 以下是我对可能的实现的评论:

  1. I don't know if the phone isn't rooted that it's possible to restore the DB file... Otherwise there aren't really any down sides for this I think... 我不知道手机是否没有根据可以恢复数据库文件...否则我认为没有真正的任何不利因素...
  2. Handling XML files on the fly on Android phones is heavy so if it can be avoided it's best not to do it like that 在Android手机上动态处理XML文件很重,如果可以避免,最好不要这样做
  3. Using the Android backup mechanism the backup feature is only available if it's enabled by the user on the phone, and all the data should be copied to the cloud... Which in my case can be in some cases quite a lot... 使用Android备份机制,备份功能仅在用户在手机上启用时才可用,并且所有数据都应该复制到云端......在我的情况下,在某些情况下可能会有很多...

I'm looking forward to see some input on this issue! 我期待在这个问题上看到一些意见!

Thanks in advance! 提前致谢!

Kr, KR,

Dirk 短剑

You don't need to have a rooted phone to restore the database from a file on the SD card. 您无需使用root电话从SD卡上的文件还原数据库。 Each application can write to it's own private directories, so you can just copy the file. 每个应用程序都可以写入自己的私有目录,因此您只需复制该文件即可。 As for 2, do you have any concrete numbers? 至于2,你有没有具体的数字? Handling XML is fairly fast, and since it's backup/restore, it doesn't happen too often and users would expect that it takes some time, so it shouldn't be a problem. 处理XML是相当快的,因为它是备份/恢复,它不会经常发生,用户会期望它需要一些时间,所以它不应该是一个问题。 As usual, measure the actual time it takes and consider how much data you have, before you make any decisions. 像往常一样,在做出任何决定之前,测量实际花费的时间并考虑您拥有的数据量。

I always use 1.). 我总是使用1.)。 Here's a class of mine that does backup of a DB to SD-card. 这是我的一类,它将DB备份到SD卡。 I'm using FileUtils from the Apache commons-io here, you need to change that if you don't use that jar. 我在这里使用来自Apache commons-io的FileUtils,你需要改变它,如果你不使用那个jar。 In addition there's need for a method in your SQLiteOpenHelper class (here MySQLiteOpenHelper.getDatabaseName()) that returns the name of your database file. 此外,SQLiteOpenHelper类(此处为MySQLiteOpenHelper.getDatabaseName())中需要一个返回数据库文件名称的方法。

You will call that from within an AsyncTask in one of your activities... 你将在你的一个活动中从AsyncTask中调用它...

public class MyDatabaseTools {
  private String appName = "";
  private String packageName = "";

  public boolean backup() {
    boolean rc = false;

    boolean writeable = isSDCardWriteable();
    if (writeable) {
      File file = new File(Environment.getDataDirectory() + "/data/" + packageName + "/databases/" + MySQLiteOpenHelper.getDatabaseName());

      File fileBackupDir = new File(Environment.getExternalStorageDirectory(), appName + "/backup");
      if (!fileBackupDir.exists()) {
        fileBackupDir.mkdirs();
      }

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, MySQLiteOpenHelper.getDatabaseName());
        try {
          fileBackup.createNewFile();
          FileUtils.copyFile(file, fileBackup);
          rc = true;
        } catch (IOException ioException) {
          //
        } catch (Exception exception) {
          //
        }
      }
    }

    return rc;
  }

  private boolean isSDCardWriteable() {
    boolean rc = false;

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      rc = true;
    }

    return rc;
  }

    public MyDatabaseTools(final Context context, final String appName) {
        this.appName = appName;
        packageName = context.getPackageName();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM