1

My app tracks school grades, calculates averages, etc. and stores all of this in a SQLite database. If a user has to reinstall or gets a new phone, I'd like to be able to restore their data.

It looks like most developers do this either by backing up to SD card or by using Android Backup Service through Google. I'm not sure which is the better method. I'd like restoring to be simple but reliable. I welcome any comments on this.

One thing I'm trying to understand is why Google says to extend BackupAgent instead of BackupAgentHelper if using a database.

If you have an SQLite database that you want to restore when the user re-installs your application, you need to build a custom BackupAgent that reads the appropriate data during a backup operation, then create your table and insert the data during a restore operation.

Why can't I just back up the database as a file and then restore the file? My SQLiteOpenHelper class already handles upgrades if db versions are different. I guess I could just abort on a downgrade.

NSouth
  • 5,067
  • 7
  • 48
  • 83

2 Answers2

0

Why can't I just back up the database as a file and then restore the file? My SQLiteOpenHelper class already handles upgrades if db versions are different. I guess I could just abort on a downgrade.

Reason: same database file may not work on different device models(even though most of the cases, it should work, there are cases where it will fail). It depends on parameters like page size etc set at sqlite engine level. Ideal way is to backup the data rather than copying the whole file

Aun
  • 1,883
  • 1
  • 17
  • 26
  • Interesting. So in what format would I back up the data so that I could restore it on any device? How is this usually done? – NSouth Nov 09 '14 at 16:13
  • usually you can take any backend server support and do the data tranfser with rest API. Use android syncadapter to sync your data to server side. – Aun Nov 09 '14 at 16:22
  • is that related to the Android Backup Service? I've never done anything with backend server support. Where should I look to start? – NSouth Nov 09 '14 at 18:17
  • Android Backup Service can be used for your scenario. You need to restore only when user changes the device or does some upgrade. -------------------------------------- Note: The backup service is not designed for synchronizing application data with other clients or saving data that you'd like to access during the normal application lifecycle. You cannot read or write backup data on demand and cannot access it in any way other than through the APIs provided by the Backup Manager. (in this case we need our own server backend) ---------------------------------------------------- – Aun Nov 10 '14 at 04:30
  • I understand that it does not serve to sync between devices. But does it take care of the issue where a database file might not work the same on different devices? Is there a special way I have to handle a database file when using this method of backup? – NSouth Nov 10 '14 at 05:22
  • mostly it should work, but i have came across models where database files were not compatible. For shared preferences they are safe. implementation details - http://stackoverflow.com/questions/5282936/android-backup-restore-how-to-backup-an-internal-database. Also read complete android doc on this topic http://developer.android.com/guide/topics/data/backup.html – Aun Nov 10 '14 at 05:32
0

It's suggested that you avoid backing up the whole db file all the time mostly because that's a lot of redundant data traffic, especially if you've only changed one record in a large db. Being able to write per-record updates to the backup system is much more efficient (though of course is not nearly as simple to implement).

ctate
  • 1,379
  • 10
  • 11
  • I don't know of another way to back up other than to file, so how would this look? Would each database entry be its own little file? My db is only 40kb, so I'm not too worried. Also, do you know if I need to put every read and write to my database in a `synchronized` statement in my program? It will take a lot of time to go through everywhere I open and close the db. Thanks! – NSouth Nov 20 '14 at 02:35