Database backup

A few months ago I made an Android app. But at that time, I did not think that I would ever need to backup my database. In truth, now I am doing this, but I have not implemented it.

Now, is there anyway for me backing up the database without losing data? Since I think that if I create a method for my database backup, when I create the application, I will lose all the data since I have this method:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + POINTS_TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + NETWORKS_TABLE);
    onCreate(db);
}
+4
source share
2 answers

The solution provided by @yuva ツ worked

Android 4 , , , root, adb, sqlite.

USB- , app.package.name .

adb backup -f ~/data.ab -noapk app.package.name " ". , . " " . , , .

data.ab android. , :

dd if = data.ab bs = 1 skip = 24 | openssl zlib -d | tar -xvf - apps/app.package.name/, , sqlite.

.

0

db SD- -

public static void copyFileInSDCard () {

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

        if (sd.canWrite ()) {
            String DATABASE_NAME = "YOURDBNAME";
            String currentDBPath = "//data//your.package//databases//" + DATABASE_NAME;
            String backupDBPath = "FILE[Name]";
            File currentDB = new File (data, currentDBPath);
            File backupDB = new File (sd, backupDBPath);

            if (currentDB.exists ()) {
                @SuppressWarnings("resource")
                FileChannel src = new FileInputStream (currentDB).getChannel ();
                @SuppressWarnings("resource")
                FileChannel dst = new FileOutputStream (backupDB).getChannel ();
                dst.transferFrom (src, 0, src.size ());
                src.close ();
                dst.close ();
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace ();
    }
}
+1

All Articles