Android Backup/Restore: How to Backup an Internal Database

Android backup/restore: how to backup an internal database?

A cleaner approach would be to create a custom BackupHelper:

public class DbBackupHelper extends FileBackupHelper {

public DbBackupHelper(Context ctx, String dbName) {
super(ctx, ctx.getDatabasePath(dbName).getAbsolutePath());
}
}

and then add it to BackupAgentHelper:

public void onCreate() {
addHelper(DATABASE, new DbBackupHelper(this, DB.FILE));
}

android - restore sqlite database from internal storge to appication

after exporting your database you can use this function to import the database, I had it testet and it works fine :

private void importDB() {
String appDataPath = getApplicationContext().getApplicationInfo().dataDir;

File dbFolder = new File(appDataPath + "/databases");//Make sure the /databases folder exists
dbFolder.mkdir();//This can be called multiple times.
//EDITED
File dbFilePath = new File(appDataPath + "/databases/"+"yourDataBaseName");

try {
//EDITED
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YourBackupFolder","yourDataBaseFileName);
FileInputStream inputStream = new FileInputStream(file); //use your database name
OutputStream outputStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer))>0)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();

} catch (IOException e){
//handle

e.printStackTrace();
}
}

Backup SQLite Database in Android

Finally, with this GitHub sample I successfully implemented whatever customization I needed. And it's live now.

How to backup/restore SQLite database on Android to Dropbox

Using the answer here, you can get a reference to your database in the form of a .db File object.

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

Once you have this, it's easy to read/write to a user's Dropbox using the Dropbox Sync API.

  • Backup: Use writeFromExistingFile() to write this local File to the Dropbox directory
  • Restore: Use getReadStream() to get a FileInputStream that can write to the appropriate local path where your .db file goes.

How to backup & restore SqliteDatabase,SharedPreferences on SD-Card - Android

You can copy your sqlite db to sdcard like this

private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.your.package" +"/databases/"+db_name;
String backupDBPath = SAMPLE_DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}

Shared Preferences file location mostly chosen by device manufacture, so it is not good to hard code path of any shared preferences file.

How to restore Sqlite database after backup Android

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

Sqlite DB Android Backup/Restore

This seems like it would be the best approach. You may want to consider taking a checksum of the SQLite file before copying and comparing it with the destination file for extra assurance. Just ensure there are no open connections to the database when you take the copy, otherwise you may end up with the DB in an unexpected state when it's restored.

The only other way I could see to do it, would be to read the actual contents of the DB and generate a file containing the SQL which which it can be restored from, this is obviously a more complex and doesn't offer any advantages to justify this complexity.



Related Topics



Leave a reply



Submit