2012-01-27 2 views
0

그래서 저는 dataBase가있는 앱을 가지고 있습니다. 이 데이터베이스는 기본 번들에 있습니다. 그렇다면이 데이터베이스를 삭제하고 인터넷에서 새 데이터베이스를 가져와야합니다. 내가 아는 한, 번들에서 아무 것도 삭제할 수 없으므로 장치 문서 디렉토리로 작업해야합니다. 하지만 인터넷에서 다운로드하지 않고 문서 디렉토리에 첫 번째 데이터베이스를 만드는 방법을 모르겠습니다. 누구든지 도와 줄 수 있습니까?장치 디렉토리

답변

0

을 읽을 액세스 할 수 있습니다 이것은 경로에 "문서"폴더를 반환이

NSString* pathToDocument = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

으로 문서화 할 수 있습니다 문서 디렉토리의 경로와 거기에 데이터베이스 파일을 배치해야합니다. 응용 프로그램의 문서 디렉토리에 복사 된 데이터베이스에서 응용 프로그램의 모든 읽기/쓰기 작업을 수행해야합니다.

+(BOOL)copyDBFileToDocumentsDirectory:(NSString*)dbFilename overwrite:(BOOL)yesNO 

그냥 확장자를 가진 파일 이름을 전달해야합니다 : 당신은 그냥이 메소드를 호출 할 필요가 :

나는 문서 디렉토리에 내 DB의 복사본을 만들려면 다음 방법을 사용합니다.

+(NSString*)databaseABSPath:(NSString*)databaseName existsInDocumentsDirectory:(BOOL)existenceStatus 
{ 
    BOOL success = NO; 

    NSString *databasePath = nil; 

    if(existenceStatus)//documents Directory 
    { 
     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDir = [documentPaths objectAtIndex:0]; 
     databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
    } 
    else //from bundle 
    { 
     NSArray *resourceSeparatedByExtension = [databaseName componentsSeparatedByString:@"."]; 

     if([resourceSeparatedByExtension count]<2) 
     { 
      NSLog(@"Problem with database name or extension"); 
      return nil; 
     } 

     databasePath = [[NSBundle mainBundle] pathForResource:[resourceSeparatedByExtension objectAtIndex:0] ofType:[resourceSeparatedByExtension objectAtIndex:1]]; 
    } 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    success = [fileManager fileExistsAtPath:databasePath]; 

    if(!success) 
    { 
     NSLog(@"Database file does not exist at path"); 
     return nil; 
    } 
    else 
    { 
     return databasePath; 
    } 


} 

+(BOOL)copyDBFileToDocumentsDirectory:(NSString*)dbFilename overwrite:(BOOL)yesNO 
{ 
    NSString *filePath = [mySQLiteDb databaseABSPath:dbFilename existsInDocumentsDirectory:NO]; 

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:dbFilename]; 

    if(filePath) 
    { 
     NSError *error = nil; 

     if(!yesNO && [[NSFileManager defaultManager] fileExistsAtPath:databasePath]) 
     { 
      NSLog(@"File already exist at path - instruction is not to overwrite"); 
      return YES; 
     } 
     else 
     { 
      [[NSFileManager defaultManager] removeItemAtPath:databasePath error:&error]; 
     } 

     error = nil; 

     [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:databasePath error:&error]; 

     if(!error) 
      return YES; 

     NSLog(@"Error copying file :\n\n%@\n\nuserinfo:%@\n\nFailure reason: %@\n\n",[error localizedDescription],[[error userInfo] description],[error localizedFailureReason]); 
    } 
    return NO; 
} 
+0

따라서이 방법은 번들의 DB를 복사합니까? – Noobass

0

번들에있는 리소스의 경로를 요청하여 번들에 연결할 수 있습니다. 예 :

NSString *path =[[NSBundle mainBundle] pathForResource:[self.imagesDict objectForKey:key] ofType:@"png"]; 
0

쉽게 경로는, 당신은 당신이 얻을 필요가 여기 & 쓰기 데이터