2012-06-06 2 views
0

iPhone 용이 앱을 작성했으며 sqlite를 사용해야합니다. 내 sqlite 데이터를 미리로드 했으므로 해당 sqlite 파일을 내 프로젝트 폴더에 복사했습니다. 그리고 응용 프로그램을 실행하면 sqlite 데이터 파일이 시뮬레이터의 응용 프로그램 파일 (문서 위치)에 복사되지 않은 것처럼 보입니다. defaultDBPath 원래 어떤 sqlite가 파일을 포함하지 않으며, 대신, 실제로 SQLite는 파일을 기다리는 것은 그것을 복사 할 시뮬레이터에서 응용 프로그램 폴더를 의미 것으로 보인다sqlite 파일을 iPhone 용 번들/앱으로 복사 할 수 없습니다.

NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"moviedbl.data"]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success){ 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"moviedbl.data"]; 
     //NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"moviedbl" ofType: @"data"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

     if(!success){ 
      NSAssert1(0, @"Failed to create writable database file with message '%'.", [error localizedDescription]); 
     } 
    } 

: 여기에 내 코드입니다. 내가 이해하는 것으로부터, 파일을 복사하기 위해 번들 (묶음이 우리 프로젝트 폴더를 참조하는지, 아니면 무엇을 프로젝트 폴더로 간주하는지 확실하지 않음)에서 파일을 가져와 시뮬레이터에 복사해야한다. 나는이 일에 붙어있어 며칠 동안 제게 계발하십시오 ... 미리 감사드립니다!

업데이트 : 결국 실제로 작동하게되었습니다. 처음에는 mainBundle이나 다른 파일에서 파일을 복사하지 않았기 때문에 그것이라고 생각했지만 실제로 .app의 실제 폴더를 열었을 때 나타났습니다. 나는 그것이 실제로 포함 된 데이터베이스를 보았다. 이상한 점은 파일 형식이 없다는 것이지만 xCode에서는 파일 형식을 "데이터"로 표시하므로 ofType : @ "data"를 계속 사용했습니다. 그래서 결국 나는 toType으로 변경했습니다 : @ ""그리고 그것은 효과가있었습니다! 어쨌든! 어리석은 나. 그러나 도와 주려고 노력했던 모두를위한 감사!! : D

+0

코드가 괜찮아 보입니다. DB 파일이 리소스에 있고 파일 이름이 맞습니까? –

+0

DB 확장을 moviedbl.data에서 moviedbl.sqlite 또는 moviedbl.sqlite3으로 변경하여 점검 했습니까? –

+0

당신의'dbPath'는 무엇입니까? – Krunal

답변

1
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 

NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"moviedbl.data"]; 

if([fileManager fileExistsAtPath:dbPath]==NO){ 

    NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"moviedbl" ofType: @"data"]; 
    BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

    if(!success){ 
     NSAssert1(0, @"Failed to create writable database file with message '%'.", [error localizedDescription]); 
    } 
} 
+0

[NSBundle mainBundle] pathForResource를 사용하여 시도 : @ "moviedbl"ofType : @ "data"]; 하지만 그것은 nil을 반환합니다 ... 왜 그런지 알겠습니까? – andy

+0

나는 데이터 파일을 내 프로젝트 위치에 저장했음을 확신한다. – andy

+0

알아 냈어! – andy

관련 문제