copy sqlite DB从Inbox到文档目录

时间:2014-06-16 14:00:23

标签: ios objective-c sqlite

当从邮件打开sqlite文件时,它来到我的文档目录中名为inbox的文件夹 我使用此代码将此文件复制到文档目录

- (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;

NSFileManager *filemgr = [NSFileManager defaultManager];

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,        YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:@"Inbox"];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"CAT.sqlite"]];

if(![filemgr fileExistsAtPath:databasePath]){

    [filemgr copyItemAtPath:inboxPath toPath:databasePath error:nil];





    NSLog( @"copy create database");
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"sucess" message:@"COPY CAT DB"         delegate:self cancelButtonTitle:@"dismis" otherButtonTitles:nil, nil];
    [alert show];
}else{
    NSLog( @"Failed to open/create database");

}

它创建名为CAT.sqlite的文件夹,在此文件夹中我找到了dataBase CAT.sqlite

如何将名为CAT.sqlite的文件从收件箱文件夹复制到文件目录,而不使用同名的创建文件夹。

1 个答案:

答案 0 :(得分:0)

这是我解决问题的代码唯一缺少的是指定我需要从收件箱文件夹中复制的文件名而不是所有文件夹而是CAT.sqlite文件夹

 - (void) copyAdd
{
NSString *docsDir;
NSArray *dirPaths;
NSString *databasePath;

NSFileManager *filemgr = [NSFileManager defaultManager];

dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
NSString* inboxPath = [docsDir stringByAppendingPathComponent:@"Inbox"];
NSString *filePath = [inboxPath stringByAppendingPathComponent:@"CAT.sqlite"];

// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"CAT.sqlite"]];

if(![filemgr fileExistsAtPath:databasePath]){

    //        NSLog(@"dbPAth : %@",dbPAth);
    [filemgr copyItemAtPath:filePath toPath:databasePath error:nil];





    NSLog( @"copy create database");
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"sucess" message:@"COPY CAT DB" delegate:self cancelButtonTitle:@"dismis" otherButtonTitles:nil, nil];
    [alert show];
}else{
    NSLog( @"Failed to open/create database");

}



}