12

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?

demongolem
  • 9,474
  • 36
  • 90
  • 105
chrisonline
  • 6,949
  • 11
  • 42
  • 62

3 Answers3

11

How can i backup my database to the sdcard automatically in my app?

Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabase objects, though.

And afterwards how do i restore it?

Copy it using standard Java I/O. Make sure you don't have any open SQLiteDatabase objects to the old database, though.

You can use getPath() on a SQLiteDatabase object to find out where it resides, AFAIK (haven't tried this).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Upvoted. Thank you you fixed my problem, for many hours I tried to backup and restore my database, the problem was I forgot to close db connections. – Omer123 Jul 14 '22 at 00:07
11

Here is my code:

    // Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();
chrisonline
  • 6,949
  • 11
  • 42
  • 62
9

Thanks to the existing answers. Here is the complete class (with usage of "getDatabasePath"):

package com.levionsoftware.bills.data.db;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.levionsoftware.bills.MyApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * Created by denny on 16/05/2016.
 * Source: http://stackoverflow.com/questions/18322401/is-it-posible-backup-and-restore-a-database-file-in-android-non-root-devices
 */
public class BackupAndRestore {
    public static void importDB(Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            if (sd.canWrite()) {
                File backupDB = context.getDatabasePath(DBHandler.getDBName());
                String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
                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();

                MyApplication.toastSomething(context, "Import Successful!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void exportDB(Context context) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String backupDBPath = String.format("%s.bak", DBHandler.getDBName());
                File currentDB = context.getDatabasePath(DBHandler.getDBName());
                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();

                MyApplication.toastSomething(context, "Backup Successful!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34