그래서 저는 dataBase가있는 앱을 가지고 있습니다. 이 데이터베이스는 기본 번들에 있습니다. 그렇다면이 데이터베이스를 삭제하고 인터넷에서 새 데이터베이스를 가져와야합니다. 내가 아는 한, 번들에서 아무 것도 삭제할 수 없으므로 장치 문서 디렉토리로 작업해야합니다. 하지만 인터넷에서 다운로드하지 않고 문서 디렉토리에 첫 번째 데이터베이스를 만드는 방법을 모르겠습니다. 누구든지 도와 줄 수 있습니까?장치 디렉토리
Q
장치 디렉토리
0
A
답변
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
번들에있는 리소스의 경로를 요청하여 번들에 연결할 수 있습니다. 예 :
NSString *path =[[NSBundle mainBundle] pathForResource:[self.imagesDict objectForKey:key] ofType:@"png"];
0
쉽게 경로는, 당신은 당신이 얻을 필요가 여기 & 쓰기 데이터
관련 문제
- 1. windows mobile 6.5 - Motorola MC55 - 장치 탐색기보기의 이상한 디렉토리 이름
- 2. 장치
- 3. 장치
- 4. 장치
- 5. UDP 장치 - 장치 SendTo 문제
- 6. 장치 토큰이 장치 ID만큼 고유합니까?
- 7. 장치 이름으로 장치 플래그를 얻으십시오.
- 8. 디렉토리
- 9. 디렉토리
- 10. 디렉토리
- 11. 디렉토리
- 12. 디렉토리
- 13. 아이폰 장치 테스트를 위해
- 14. 시간 소인을 기반으로하는 디렉토리 디렉토리
- 15. USB 저장 장치 SerialNumber (USB 장치 설명자에서)
- 16. OpenCL CPU 장치 대 GPU 장치
- 17. 레퍼런스 레이저 장치 3D 장치 생성 실패 -
- 18. 잠금 장치 무료 할당 장치 구현 - 올바른지?
- 19. 기존 장치 드라이버를 통한 장치 액세스
- 20. .htaccess 디렉토리 1을 디렉토리 2와 일치하지 않는 디렉토리 2로 리디렉션합니다.
- 21. 가능성이 장치
- 22. 장치 정보
- 23. 유지 장치
- 24. 안드로이드 : 장치
- 25. 장치 찾기
- 26. 장치 드라이버
- 27. IOS : 장치
- 28. 장치 핸들
- 29. 날짜 장치
- 30. 안드로이드 장치
따라서이 방법은 번들의 DB를 복사합니까? – Noobass