2015-07-03 180 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

相關問題