2012-03-27 2 views
0

SQLite 데이터베이스를로드하는 다음 코드에 문제가 있습니다.Sqlite 데이터베이스로드 실패 - sqlite 명령문 문제 - iPhone - xCode 4.3.1

- (NSArray *)getDatabase { 

NSLog(@"Get Database Called"); 

    NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease]; 
    NSString *query = @"SELECT Description, UniqueID, HexCoords, NoHexCoords, NoAnnots, AnnotText, DescriptionFormatting, HexCoordsPortrait FROM MainTable"; 
    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) 
     == SQLITE_OK) { 
     while (sqlite3_step(statement) == SQLITE_ROW) { 

      char *nameChars = (char *) sqlite3_column_text(statement, 0); 
      char *desChars = (char *) sqlite3_column_text(statement, 1); 
      int uniqueID = sqlite3_column_int(statement, 2); 

중단 점을 사용에서 나는 문제가있는 경우 문 및 문 경우 코드는 결코이 과거 얻을 수 없다는 것을 알 수 있습니다. 아무도 무엇이 잘못되었을 지 알아낼 수 있습니까? 코드는 몇 달 전부터 작동했으며 최근에 xCode 4.3으로 업그레이드하여 문제가 될 수 있습니까?

감사합니다.

+0

내가 제대로 _database 설정되지 않은 경우이이 같은 행동을 볼 수있는 유일한 이유. 데이터베이스 열기를위한 호출이 아마도 실패합니까? 또한, 마지막 인자는 객체에 대한 포인터가 아니므로'nil'이 아니라'NULL'이어야합니다. 두 가지 모두 현재 0으로 컴파일되므로 문제가 발생하지 않아야하지만 동일하게 유지된다는 보장은 없습니다. –

답변

1

네, 요아킴에 동의합니다. 때때로 DB가 실제로 연결되지 않는 문제가 있습니다. 내가하는 일은 두 가지입니다. 먼저 응용 프로그램 위임에 다음 코드를 추가합니다.

- (void) copyDatabaseIfNeeded { 

    //Using NSFileManager we can perform many file system operations. 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) { 

     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDB.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

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

이 함수를 ApplicationDidFinishLaunching에서 호출하십시오.

이제 ur 번들에있는 데이터베이스를 제거하십시오. (백업을했는지 확인하십시오). 그리고 가능한 경우 (Iphone 시뮬레이터 폴더에서 모든 프로젝트 삭제) Coz가 이전 데이터베이스가 첨부되는 경우가 있습니다. 프로젝트를 비우고, 번들로 데이터베이스를 추가하십시오. 이

을 일했다면

이 알려 .. 컴파일 가져 오기 경로 기능

- (NSString *) getDBPath { 
     //Search for standard documents using NSSearchPathForDirectoriesInDomains 
     //First Param = Searching the documents directory 
     //Second Param = Searching the Users directory and not the System 
     //Expand any tildes and identify home directories. 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 
     return [documentsDir stringByAppendingPathComponent:@"MYDB.sqlite"]; 
} 
+0

감사합니다.하지만 "getDBPath"메서드는 무엇입니까? – GuybrushThreepwood

+0

죄송합니다. – WaaleedKhan

+0

을 언급하는 것을 잊어 버렸습니다. 제가 추가하고 싶은 한가지 더 .. u 질문에 게시 된 u 함수에 (NSArray *) 반환 유형을 설정했습니다. 최상의 방법은 아닙니다 .. U는 NSMutableArray를 사용해야합니다. – WaaleedKhan