2013-07-22 12 views
0

데이터베이스 (sqlite 파일)에서 데이터를 가져 오는 DBManager이 있습니다. 다른 모든 쿼리는 잘하지만,이 사람은 어떻게 든 작동하지 않는 것 같다Sqlite 데이터베이스에서 열을 읽을 수 없습니다.

-(NSArray *)readCountries{ 
NSLog(@"[DBManager] readCountries"); 
NSMutableArray *countriesArray = [[NSMutableArray alloc] init]; 
//open db from users filesystem 
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 
    const char* sql = "SELECT DISTINCT country FROM aed ORDER BY rowid"; 
    sqlite3_stmt *statement; 
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) { 
     //loop through results 
     while (sqlite3_step(statement) == SQLITE_ROW) { 
      //read data from record 
      NSString *_country; 
      char* tmpCountry = (char*)sqlite3_column_text(statement, 1); 
      NSLog(@"tmpCountry = %@", [NSString stringWithUTF8String:tmpCountry]); 
      if (tmpCountry != NULL) { 
       _country = [NSString stringWithUTF8String:tmpCountry]; 
      }else{ 
       _country = @"n/a"; 
      } 
      NSLog(@"country = %@", _country); 
      [countriesArray addObject:_country]; 
     } 
    } 
    //finalize statement 
    sqlite3_finalize(statement); 
} 
//close database 
sqlite3_close(database); 
NSLog(@"[DBManager] countriesArray has %d objects", [countriesArray count]); 
return (NSArray*)countriesArray; 

}

모든 내 배열 괜찮 5 개체를 가지고, 로그에서 얻을 -하지만 그것이 될 souldn't "n/a"... 어떤 생각? 다른 쿼리는 좋은데, 주로 sqlite3_column_text을 사용하므로 이해가되지 않습니다. 왜 여기에서 작동하지 않습니다. 아마도 신선한 눈이 도움이 될 것입니다.

+0

rmaddy가 로깅에 대해 말한 바는 무엇이 잘못되었는지를 알려주는 대신 코드가 자동으로 실패하게됩니다. (그리고 sqlite3_errmsg는 종종 유익한 정보입니다.) –

답변

1

이것은 C-API sqlite과 혼동스럽게 일치하지 않습니다. sqlite3_column_xxx 함수를 사용할 때 열 인덱스는 0부터 시작합니다. 그러나 sqlite3_bind_xxx 함수의 경우 열 인덱스는 1부터 시작합니다.

변경이 :

char* tmpCountry = (char*)sqlite3_column_text(statement, 1); 

로는 :

char* tmpCountry = (char*)sqlite3_column_text(statement, 0); 

BTW - 당신은 당신의 sqlite3_opensqlite3_prepare 통화를 else 문을 추가해야합니다. 실패 할 경우 sqlite3_errmsg 기능을 사용하여 오류를 기록 할 수 있습니다.

+0

sqlite3_step의 반환 코드도 확인해야합니다. 오류가 발생하기 전에 SQLite_DONE을 필터링해야하기 때문에 좀 더 많은 작업이 필요합니다. –

+0

고마워, 내가 어떤 점에서 0을 가졌지 만 지금은 작동하지 않았다. xcode도 잠자기 시간이 필요해 보인다.) – raistlin

관련 문제