2

In my app, I would like to recognize when the database has been damaged and automatically repair it. Specifically, when the app is first opened I run an ANALYZE command and if it comes back with status 11 (database disk image is malformed), then I would like to have a way of recovering the data.

All the examples I find use the terminal to do a dump and then import of the subsequent SQL command file.

How can I do a dump of the db from within my own code since the command line isn't available on iOS?

Thanks

Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70
  • The `.dump` often does not work; it simply outputs everything that happens to be readable. – CL. Feb 20 '16 at 14:00

1 Answers1

1

I think you can easily copy and save the database with another name (extend timestamp). You have to figure out when it is damaging(It should not damage it depends on your meaning of damage) and replace it.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *sqlpath = [documentsDirectory stringByAppendingPathComponent:@"Your-sqlitefile"];
NSString *sqldump = //append timestamp

[fileManager copyItemAtPath:sqlpath toPath:sqldump error:&error];

Edit:

Please refer below answer. I have not used by myself but hope it helps. https://stackoverflow.com/a/27453637/1881472

Community
  • 1
  • 1
codester
  • 36,891
  • 10
  • 74
  • 72