【问题标题】:Failed to create sqlite database in a sub folder of documents directory在文档目录的子文件夹中创建 sqlite 数据库失败
【发布时间】:2015-07-03 18:23:10
【问题描述】:

我正在尝试在我的 iOS 应用程序中使用 Sqlite3 创建数据库。如果我在文档目录的子文件夹中创建数据库,它不会创建数据库,它无法打开或创建数据库。如果我不在子文件夹中创建数据库,而是直接在文档目录中创建它。它正在正确创建它。

它给出的路径为 databasePath: "/var/mobile/Containers/Data/Application/986037DB-6FA0-4066-9977-C5D7A075C5E7/Do‌​cuments/MailFolder/INBOX.db" 但它失败了 -> if (sqlite3_open(dbpath, &database) == SQLITE_OK) – 下面是我在子文件夹中创建数据库的代码。有人可以纠正我这里有什么问题吗?

- (BOOL) createFolderMailDB :(NSString *) dbName {
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    //docsDir =[[dirPaths objectAtIndex:0] stringByAppendingPathComponent:@"MailFolder"];
    NSString *documentsPath = [dirPaths objectAtIndex:0];
    docsDir = [documentsPath stringByAppendingPathComponent:@"/MailFolder"];

    //docsDir = dirPaths[0];
    // Build the path to the database file
    NSMutableString *finalDBPath = [[NSMutableString alloc]init];
    [finalDBPath appendString:dbName];
    [finalDBPath appendString:@".db"];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: finalDBPath]];

    NSLog(@"databasePath: %@", databasePath);

    BOOL isSuccess = YES;

    NSFileManager *filemgr = [NSFileManager defaultManager];
    if ([filemgr fileExistsAtPath: databasePath] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &database) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "create table if not exists MailFolderDBTable (emailid text, foldername text, messagedata text)";
            if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
                != SQLITE_OK)
            {
                isSuccess = NO;
                NSLog(@"Failed to create table");
            }
            sqlite3_close(database);
            return  isSuccess;
        }
        else {
            isSuccess = NO;
            NSLog(@"Failed to open/create database");
        }
    }

    return isSuccess;
}

【问题讨论】:

  • 您需要先创建子文件夹。
  • 顺便说一句 - 去掉调用 stringByAppendingPathComponent 中的“斜线”。该方法的重点是确保为您添加正确的路径分隔符。
  • 我只先创建子文件夹。它给出的路径为“/var/mobile/Containers/Data/Application/986037DB-6FA0-4066-9977-C5D7A075C5E7/Documents/MailFolder/INBOX.db” 但在这里失败->if (sqlite3_open(dbpath, &database) == SQLITE_OK)
  • 不,您必须在文件系统中实际创建文件夹。
  • 不相关,但是如果你使用sqlite3_exec的error参数,(a)你不妨NSLog这样你就知道为什么失败了; (b) 调用该参数的sqlite3_free,否则您将泄漏该内存。

标签: ios objective-c sqlite


【解决方案1】:

您放置此数据库的文件夹必须存在,否则尝试在其中创建数据库将失败。因此,在继续创建数据库之前创建该文件夹:

NSString *documentsPath  = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *databaseFolder = [documentsPath stringByAppendingPathComponent:@"MailFolder"];
NSFileManager *filemgr   = [NSFileManager defaultManager];

if (![filemgr fileExistsAtPath:databaseFolder]) {
    NSError *error;
    if (![filemgr createDirectoryAtPath:databaseFolder withIntermediateDirectories:FALSE attributes:nil error:&error]) {
        NSLog(@"Error creating %@: %@", databaseFolder, error);
    }
}

NSString *databasePath = [[databaseFolder stringByAppendingPathComponent:dbName] stringByAppendingPathExtension:@"db"];

if (![filemgr fileExistsAtPath:databasePath]) {
    // database creation logic
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2013-08-06
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多