将 sqlite DB 从收件箱复制到文档目录

Posted

技术标签:

【中文标题】将 sqlite DB 从收件箱复制到文档目录【英文标题】:copy sqlite DB From Inbox to documents directory 【发布时间】:2014-06-16 14:00:23 【问题描述】:

当从邮件中打开 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"];

// 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 的文件夹,在这个文件夹中我找到了数据库 CAT.sqlite

如何在不创建同名文件夹的情况下将名为 CAT.sqlite 的文件从收件箱文件夹复制到文档目录。

【问题讨论】:

问题是什么? 需要将数据库复制到文档目录而不创建任何文件夹,并且收到新的同名数据库时需要从收件箱中删除旧数据库 如果您有特定的问题需要解决,那么这是您提出问题的正确网站。但是,我们不是在这里代表您实现功能;我们不为你工作。因此,请尝试使用您面临的具体问题来编辑您的问题,包括有关问题的详细信息以及您收到的任何错误消息。 我想我会问一些具体的事情,那就是为什么要创建文件夹而不是复制数据库,并且我输入了我使用的代码,所以如果你不知道为什么,请不要打扰我 您发布的代码尝试将Indbox 文件夹复制到名为CAT.sqlite 的文件中。您需要 Inbox 文件夹中的文件路径,并且您想要移动该文件,而不是 Inbox 文件夹。 【参考方案1】:

这是我解决问题的代码,唯一缺少的是指定我需要从收件箱文件夹复制的文件名,而不是所有文件夹,而是 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");






【讨论】:

以上是关于将 sqlite DB 从收件箱复制到文档目录的主要内容,如果未能解决你的问题,请参考以下文章

如何将 PreCreated 和 Pre Populated 完整的 sqlite database.db 文件复制到 iOS 应用程序中

如何使用 XCode 菜单而不是以编程方式将我的 sqlite 文件从文档目录复制到应用程序包

为啥不在swift 3中将database.db从bundle复制到文档目录? [复制]

xcode sqlite3 如何将数据库从捆绑包复制到文档文件夹?

为啥在使用代码将 db 复制到文档文件夹之前 Xcode 会自动复制 db 并且它是空的?

PyQt5 - 从 sqlite3 复制到 QTableWidget 的 SQL 行不显示