Dowemo
0 0 0 0


Question:

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

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

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


Best Answer:


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



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs