Backup/restore sqlite db in android [duplicate]

In the folder “/data/data/’your.app.package’/databases/” you have a .db file that is your database. You can copy that file, save it, and then place it back there again.

One example on how to backup the database to the external storage:

    final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";

    // Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0){
        output.write(buffer, 0, length);
    }

    // Close the streams
    output.flush();
    output.close();
    fis.close();

Leave a Comment