Wednesday, 10 October 2012

SqliteDb with Dropbox API | Import/Export | Sync With Dropbox


Here we have a tutorial for importing & exporting your  file onto Dropbox.
Demonstration for sqlite file.

In your Interface file
DBRestClient* restClient;
NSArray *backUps;
NSString* currentDBPath;
In your Implementation file
#pragma mark View-Did Load
- (void)viewDidLoad {
 currentDBPath = [self dbPath];
}
#pragma mark DB-Path

- (NSString *) dbPath{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *localDBPath = [documentsDirectory stringByAppendingPathComponent: @"/YOUR.sqlite"];
 return localDBPath;
}
#pragma mark DBRestClient
Use didPressLink method for authentication/session of DBRestClient
- (void)didPressLink {
if (![[DBSession sharedSession] isLinked]) {
     NSLog(@"Logged in");
 [[DBSession sharedSession] linkFromController:self];
 } else {
       NSLog(@"Logged out");
 [[DBSession sharedSession] unlinkAll];
}
}

- (DBRestClient*)restClient {
 if (restClient == nil) {
       restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
       restClient.delegate = self;
    }
   return restClient;
}

#pragma mark Upload a file
For uploading file, here we save the file with name "db_DATE_FORMAT.sqlite" and uploading to your destination directory name "DB_BACKUP"

- (IBAction)upLoadFile:(id)sender{
 NSDateFormatter *format = [[NSDateFormatter alloc] init];
 [format setDateFormat:@"yyyyMMdd-HH:mm"];
 NSDate *now = [NSDate date];
 NSString *dateString = [format stringFromDate:now];
 NSString *filename = [NSString stringWithFormat:@"db_%@.sqlite",dateString];
   NSLog(@"file name : %@", filename);
  NSString *destDir = @"/DB_BACKUP";
  [[self restClient] uploadFile:filename toPath:destDir withParentRev:nil fromPath:currentDBPath];
}

- (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath from:(NSString*)srcPath metadata:(DBMetadata*)metadata {
      NSLog(@"File uploaded successfully to path: %@", metadata.path);
}

- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error { 
         NSLog(@"File upload failed with error - %@", error);
}

#pragma mark list of folders
Load the data from destination directory DB_BACKUP.

- (IBAction)downloadingFile:(id)sender{
   [[self restClient] loadMetadata:@"/DB_BACKUP"];
}

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
Finding with .sqlite extensions here.
 if (metadata.isDirectory) {
NSLog(@"Folder '%@' contains:", metadata.path);
 NSArray* validExtensions = [NSArray arrayWithObjects:@"sqlite", nil];
 NSMutableArray* newDBPath = [NSMutableArray new];
 for (DBMetadata* child in metadata.contents) {
 NSString* extension = [[child.path pathExtension] lowercaseString];
 if (!child.isDirectory && [validExtensions indexOfObject:extension] !=  NSNotFound) {
  [newDBPath addObject:child.path];
}
}
[backUps release];
backUps = newDBPath;
 NSLog(@"Count : %i", [backUps count]);
 }
Here we load the first file in the directory, if you want anymore then change the objectAtIndex accordingly.
 NSLog(@"Back - up %i, filename : %@", [backUps count],[backUps objectAtIndex:0]);
 [[self restClient] loadFile:[backUps objectAtIndex:0] intoPath:currentDBPath];
}

- (void)restClient:(DBRestClient *)client
loadMetadataFailedWithError:(NSError *)error 
   NSLog(@"Error loading metadata: %@", error);
}

#pragma mark Download a file

- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath {
   NSLog(@"File loaded into path: %@", localPath);
}

- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error {
   NSLog(@"There was an error loading the file - %@", error);
}

No comments:

Post a Comment