Backup in Sqlite database

Asked

Viewed 2,666 times

5

I need to backup an Android Sqlite database.

Ex: I will change device and need to copy the application BD to be loaded on other Android phone.

Is there any viable solution?

I developed an application that uses Sqlite to record information, now I need to make the user can make a backup of the bank to be able to transfer to another mobile.

  • Not a duplicate of http://answall.com/questions/33387/comorcopiar-banco-sqlite-da-memoria-interna-para-o-cart%C3%A3o-sd-comando-linux/33481#33481?

  • I need to backup for everyone who has the application installed!

  • Ah, it wasn’t clear from the question. You can use the BackupHelper in that case: http://developer.android.com/reference/android/app/backup/BackupHelper.html

  • doubt of it I believe is how to export the file and how to import.

  • 1

    You have a good coded help with this post http://www.techrepublic.com/blog/software-engineer/export-sqlite-data-from-your-android-device/

  • how it wants to save the bank and transfer to another cell phone, without it exporting to a file and importing? Besides that it did not make clear

  • would be just that, simply export to SD or anything else, and then import again so the application can load!

  • I have the same problem but reissued the question, so I created a slightly different one http://answall.com/questions/38422/exportar-arquivos-do-sqlite-para-csv @Check it out and see if anything helps you

Show 3 more comments

2 answers

8

The solution below has been extracted from a response in SOEN and does what you want, although there is plenty of room for improvement and better portability:

Import

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

    if (sd.canWrite()) {
        String currentDBPath = "//data//" + "<nome do package>" 
            + "//databases//" + "<nome da BD>";
        String backupDBPath = "<nome ficheiro backup da BD>"; // No SDCard
        File backupDB = new File(data, currentDBPath);
        File currentDB = 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(), "Importação com sucesso!",
            Toast.LENGTH_SHORT).show();

    }
  }
  catch (Exception e) {

    Toast.makeText(getApplicationContext(), "Importação Falhou!", Toast.LENGTH_SHORT)
        .show();
  }
}

Export

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

      if (sd.canWrite()) {
          String currentDBPath = "//data//" + "<nome do package>"
              + "//databases//" + "<nome da BD>";
          String backupDBPath = "<destino>";
          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 com sucesso!",
              Toast.LENGTH_SHORT).show();
      }
  }
  catch (Exception e) {
      Toast.makeText(getApplicationContext(), "Backup Falhou!", Toast.LENGTH_SHORT)
          .show();   
  }
}

User response credits @adefran83 in this answer in the SOEN.

0

In the case of getApplicationContext(), it will be interesting for you to pass as parameter to the constructor of the class where these methods will be Context, so you can access getApplicationContext().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.