0

Imagine the following scenario (I allow backup / restore from my app, I'm doing backup / restore white the entire file.db):

The user make backup of the database. In the future I do an update on my application which has a new version of the database. what would happen if the user restore the database with the old version?

How can I avoid this kind of problem?

It would be a good idea to use BackupHelper? I did a simulation of the worst scenario of my database and gave 20k, BackupHelper is recommended for less than 1mb, it would be a good idea to use it? I think my app will never exceed 100kb.

weldsonandrade
  • 606
  • 1
  • 9
  • 25

2 Answers2

1

May be this is not the best approach but you can do it as:

1- Store the DB Version in the database.

2- After restoring the database, check the DB Version and do the required changes accordingly. Like

void afterRestoration()
{
   long dbVersion = get from db;
   if(dbVersion == 1)
   {
     alter table1 add column1;
   }
   else
   {
   }
}
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • Thanks It can work =). Hey what you think about use BackupHelper to make the backup and restore? I did a simulation of the worst scenario of my database and gave 20k, BackupHelper is recommended for less than 1mb, it would be a good idea to use it? I think my app will never exceed 100kb. – weldsonandrade Nov 12 '12 at 13:11
  • I have not used it!! but from the documentation it looks a good solution to me. – Yaqub Ahmad Nov 12 '12 at 13:15
1

You access SQLite databases via a SQLiteOpenHelper, which provides three callbacks: onCreate(), onUpgrade() and onOpen().

The constructor declares a version parameter, and internally it compares the database version (which is stored in the file) with the supplied argument. If the requested version is newer than the file's, your onUpgrade(db, old, new) is called and you get a chance to alter tables, fill rows and so on.

The restore procedure should simply close all open cursors and helpers and copy the new file in.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Thanks It can work =). Hey what you think about use BackupHelper to make the backup and restore? I did a simulation of the worst scenario of my database and gave 20k, BackupHelper is recommended for less than 1mb, it would be a good idea to use it? I think my app will never exceed 100kb. – weldsonandrade Nov 12 '12 at 13:16
  • It depends. The backup framework is intended to be an automated way to make users data survive factory resets and device upgrades. If that's what you want, it's ok (the file size doesn't really matters here). If you need multiple clients synchronization or other related features, you have to code them yourself. I'd say, start with the framework because it's quick and works, and see how your application evolves and what your users want – Raffaele Nov 12 '12 at 13:27