2011-01-27 2 views
1

새로운 iPhone 프로젝트에서 작업 중이며 sqlite에 문제가 있습니다. 이전에 다른 프로젝트에서이 작업을 수행 했으므로 제대로 작동 했으므로 정확히 어떤 작업이 진행되고 있는지 확실하지 않습니다. 이전과 같은 코드를 사용하고 있지만 상황은 조금 다릅니다.iPhone 시뮬레이터를 사용한 SQLite 오류/가능한 사용 권한 문제

우선, 단위 테스트를 사용하여 이번에는 Cocoa Unit Test Bundle을 만들려고 노력하고 있습니다. 올바르게 작동하고있어 sqlite 데이터베이스에 대한 단위 테스트를 만들고 싶습니다.

int result = sqlite3_open([databasePath UTF8String], &database); 

이 매번 실패

-(void) checkAndCreateDatabase{ 
     BOOL success; 

     // Create a FileManager object, we will use this to check the status 
     // of the database and to copy it over if required 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     // Check if the database has already been created in the users filesystem 
     success = [fileManager fileExistsAtPath:databasePath]; 

     // If the database already exists then return without doing anything 
     if(success) return; 

     // If not then proceed to copy the database from the application to the users filesystem 

     // Get the path to the database in the application package 
     NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

     // Copy the database from the package to the users filesystem 
     [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

     [fileManager release]; 
    } 

가 그럼 난 다음 줄에 데이터베이스를 열려고 :이 테스트를 실행

우선 다음과 같다 [자기 checkAndCreateDatabase]입니다 오류 코드 14 SQLITE_CANTOPEN이고 databasePath는 "/ Users/labuser/Library/Application Support/iPhone Simulator/Documents/projectfusion.db3"입니다.

이상한 점은 내가 그 디렉토리로 갈 때 Documents /가 없기 때문에 내가 만들면 실패하지 않는다는 것입니다. projectfusion.db3의 크기는 0kb입니다. 그 테이블은 거기에 없다. 테이블이 없으므로 모든 sqlite3_prepare_v2()가 실패합니다. projectfusion.db3 파일을 실행하기 전에 수동으로 해당 디렉토리에 복사하면 올바르게 작동합니다.

내가 단위 테스트에서 이것을하고 있기 때문에 스크립트에 권한이 없거나 뭔가가 있습니까? 또는 내 대학의 학교 컴퓨터에서 작업 중이므로 해당 디렉토리에 쓸 수 없기 때문에 가능합니까? (나는 관리자로 로그인을 시도했지만 어느 쪽도 작동하지 않았다.)

답변

0

는 아래의 코드를 시도하여 해당 경로를 얻을 수

내가 밖으로 코드를 주석 유지 할 필요가 없습니다 TEST라는 매크로를 설정 한

.

 
- (NSString *)createEditableCopyOfDatabaseIfNeeded { 
    // First, test for existence. 
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    #ifdef TEST 
    documentsDirectory = [[NSFileManager defaultManager] currentDirectoryPath]; 
    #endif 


    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DBNAME]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) { return writableDBPath;} ; 
    // The writable database does not exist, so copy the default to the appropriate location. 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DBNAME];  
    #ifdef TEST 
    defaultDBPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"YOURDB" ofType:@"sqlite"]; 
    #endif 

    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
    return (defaultDBPath); 
} 
0

xcode의 응용 프로그램 번들에 데이터베이스를 추가 한 다음 응용 프로그램 Documents 디렉토리로 복사 해보십시오. 당신은

- (NSString *)applicationDocumentsDirectory { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
return basePath; 
} 
+0

왜 내가 이미하고있는 것과 다른지는 알 수 없습니다. 위의 코드를 위의 코드로 바꿔서 시도했습니다. NSString * databasePathFromApp = [NSString stringWithFormat : @ "% @/% @", [self applicationDocumentsDirectory], databaseName]; 이고 반환 된 경로는 위와 동일하지만 "/ Users/labuser/Library/Application Support/iPhone Simulator/Documents/projectfusion.db3"입니다. "응용 프로그램 번들에 데이터베이스 추가"로 가정합니다. 기존 파일로 추가하는 것입니까? 내 "Resources"디렉토리에서 그렇게했습니다. – gameburke

+0

다음과 같이 smth를 반환해야합니다./Users/max/Library/Application Support/iPhone Simulator/4.2/Applications/C5178257-E12C-4088-BA2C-68E472458575/Documents 정말 이상합니다. 좋아, 앱 번들에서 DB에 액세스 할 수 있습니까? – Max

+0

Build/Debug-iphonesimulator/ProjectFusion에서 Finder를 찾았습니다. ProjectFusion에 "패키지 내용보기"가 있었는데 거기에있었습니다. 이게 네가 말하는거야? – gameburke

관련 문제