vic3ai
vic3ai

Reputation: 279

How to backup and restore sqlite3 file from iOS app

I plan to develop a Point of Sales system, and intend to use sqlite3 as the database. After few days research, I can't find any tutorial or example on backup or restore sqlite3 database. The solution I can accept is either upload to cloud or dropbox.

Upvotes: 1

Views: 1755

Answers (1)

Balaji Ramakrishnan
Balaji Ramakrishnan

Reputation: 1949

Try this answer.

Creating Backup & Restoring file using Dropbox

Follow the steps that appears in Dropbox integration for your iOS App.

First, Get your local db path as given below:

NSArray *docsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docPath = [docsDirectory objectAtIndex:0];

databasePath = [docPath stringByAppendingPathComponent:@"filename.sqlite"];

NSLog(@"db path -> %@",databasePath);

Then Upload that file to dropbox as following:

    NSString *fileName = @"filename.sqlite";
    NSString *destDir = @"/";

    if (appDelegateObj.parentRevId == nil)
    {
            //Uploads a fresh file.
            [appDelegateObj.restClientObj uploadFile:fileName toPath:destDir withParentRev:nil fromPath:databasePath];
    }
    else
    {
    // Uploads a file with already existing file
            [appDelegateObj.restClientObj uploadFile:fileName toPath:destDir withParentRev:appDelegateObj.parentRevId fromPath:databasePath];
    }

Here, parentRevId is for identifier of existing file.

You can get the parentRevId from Dropbox delegate method.

And then Restore the dropbox file to your local db path as following:

 if (appDelegateObj.parentRevId == NULL) // No such File found
 {
       [SVProgressHUD showErrorWithStatus:@"No Data in Dropbox" maskType:SVProgressHUDMaskTypeBlack]; // Activity Indicator
 }
 else
 {
       NSString *destDir = @"/filename.sqlite";                             
       [appDelegateObj.restClientObj loadFile:destDir intoPath:databasePath];
 }
  • restClientObj is the object of DBRestClient (Dropbox)

Upvotes: 3

Related Questions