2015-07-03 100 views
1

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

这是给路径databasePath: “在/ var /移动/集装箱/数据/应用/ 986037DB-6FA0-4066-9977-C5D7A075C5E7 /做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; 
} 
+0

您需要先创建子文件夹。 – rmaddy

+0

顺便说一句 - 摆脱“stringByAppendingPathComponent”调用中的“斜线”。该方法的重点在于确保为您添加正确的路径分隔符。 – rmaddy

+0

我只是首先创建子文件夹。它是给路径为“/var/mobile/Containers/Data/Application/986037DB-6FA0-4066-9977-C5D7A075C5E7/Documents/MailFolder/INBOX.db”但这里是失败 - > if(sqlite3_open(dbpath,&database) == SQLITE_OK) – user1953977

回答

3

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

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 
} 
+1

解决了,非常感谢! – user1953977

相关问题