Backup Room database

后端 未结 7 800
夕颜
夕颜 2020-12-08 01:41

I\'m trying to backup a room database programmatically.

For that, I\'m simply copying the .sqlite file that contains the whole database

But, bef

7条回答
  •  眼角桃花
    2020-12-08 02:05

    To more specifically answer your question, this is how I backup the room database in one of my Apps.

    1. Check for permission to read from / write to the external storage. You can ignore this step if you write to your App files directory.
    2. Close your RoomDatabase. In my case AppDatabase refers to a singleton that contains logic for building the room database initially. AppDatabase.getInstance(this).getDatabase() gets the current instance of the singleton, and its current database class, that extends from RoomDatabase. This essentially calls RoomDatabase.close().
    3. Define the source and destination files, depending on backing up or restoring. I include shm and wal files, even though they are temporary files.
    4. Copy the files with your method of choice. FileUtils in this case, refers to commons-io.

    The code

    if(id == R.id.action_save_db) {
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if(permission == PackageManager.PERMISSION_GRANTED) {
            AppDatabase.getInstance(this).getDatabase().close();
    
            File db = getDatabasePath("my-db");
            File dbShm = new File(db.getParent(), "my-db-shm");
            File dbWal = new File(db.getParent(), "my-db-wal");
    
            File db2 = new File("/sdcard/", "my-db");
            File dbShm2 = new File(db2.getParent(), "my-db-shm");
            File dbWal2 = new File(db2.getParent(), "my-db-wal");
    
            try {
                FileUtils.copyFile(db, db2);
                FileUtils.copyFile(dbShm, dbShm2);
                FileUtils.copyFile(dbWal, dbWal2);
            } catch (Exception e) {
                Log.e("SAVEDB", e.toString());
            }
        } else {
            Snackbar.make(mDrawer, "Please allow access to your storage", Snackbar.LENGTH_LONG)
                    .setAction("Allow", view -> ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    }, 0)).show();
        }
    } else if(id == R.id.action_load_db) {
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if(permission == PackageManager.PERMISSION_GRANTED) {
            AppDatabase.getInstance(this).getDatabase().close();
    
            File db = new File("/sdcard/", "my-db");
            File dbShm = new File(db.getParent(), "my-db-shm");
            File dbWal = new File(db.getParent(), "my-db-wal");
    
            File db2 = getDatabasePath("my-db");
            File dbShm2 = new File(db2.getParent(), "my-db-shm");
            File dbWal2 = new File(db2.getParent(), "my-db-wal");
    
            try {
                FileUtils.copyFile(db, db2);
                FileUtils.copyFile(dbShm, dbShm2);
                FileUtils.copyFile(dbWal, dbWal2);
            } catch (Exception e) {
                Loge("RESTOREDB", e.toString());
            }
        } else {
            Snackbar.make(mDrawer, "Please allow access to your storage", Snackbar.LENGTH_LONG)
                    .setAction("Allow", view -> ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.READ_EXTERNAL_STORAGE
                    }, 0)).show();
        }
     }
    

提交回复
热议问题