3

我正在开发一个 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”文件。请不要将其视为重复的问题。我浪费了一天的时间进行谷歌搜索,但没有找到解决方案。请指导我。

提前致谢

4

1 回答 1

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;
}
于 2013-07-24T10:52:55.387 回答