如何让DropBox中的SQLITE数据库文件同步?

时间:2013-07-17 14:53:25

标签: iphone objective-c sqlite dropbox nsfilemanager

我正在研究一个IPad应用程序。我想将整个数据库文件上传到dropbox。我在google上搜索但没有找到合适的解决方案。我使用以下代码创建数据库。

-(BOOL) createDatabaseFile
{
     NSString *docsDir;
NSArray *dirPaths;
NSFileManager *filemgr = [NSFileManager defaultManager];
BOOL successMsg = YES;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];

// Build the path to the database file
self.detaildatabasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@`"Database.sql"`]];
// Check For Existence of the database file
if ([filemgr fileExistsAtPath:self.detaildatabasePath])
{
    //File Exists At The Path
}
else
{
    //Since file is not available at the path create a Database File
    successMsg = [filemgr createFileAtPath:self.detaildatabasePath contents:[NSData data] attributes:nil];
}
return successMsg;
}

那么如何获取我的“Database.sql”文件。请不要将其视为重复的问题。我浪费了一天的谷歌搜索,但没有找到解决方案。请指导我。

提前致谢

1 个答案:

答案 0 :(得分:0)

要将文件上传到Dropbox,您需要Dropbox SDK - https://www.dropbox.com/developers/sync/sdks/ios(如何将Dropbox.framework添加到您的项目中)

然后你可以上传文件:

-(void) createBackupInDropbox {
    NSString *filename = @"backup.sqlite"; //File name in DropBox
    NSString *destDir = @"/backups"; //Destination path in DropBox
    NSString *fromPath = @"..."; //local path to your DB file
    /*
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *docsPath = [paths objectAtIndex:0];
     fromPath = [docsPath stringByAppendingPathComponent:@"my_database.sqlite"];
    */

    [[self restClient] deletePath:[NSString stringWithFormat:@"%@/%@", destDir, filename]]; //delete file if you need to replace it
    [[self restClient] uploadFile:filename toPath:destDir
                    withParentRev:nil fromPath:fromPath];
}

控制上传过程:

- (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 {
    [waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
    NSLog(@"File upload failed with error - %@", error);
}

- (void)restClient:(DBRestClient*)client uploadProgress:(CGFloat)progress forFile:(NSString *)destPath from:(NSString *)srcPath
{

}

和restClient方法:

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