2010-03-01 2 views
0

몇 가지 도움이 필요합니다! 부디. 나는 아주 간단한 응용 프로그램, 기본적으로 간단한 책 독자를 개발하려고합니다. 이상적으로, 첫 번째보기는 챕터 이름을 나열하는 uitableview가 될 것이고 행을 선택하면 챕터가 세분화 된 세부보기로 이동합니다. 세그먼트는 실제로 테이블 내부의 자체 레코드입니다. 그래서 db 테이블의 여러 레코드가있는 uitext 뷰를 채우려고합니다. 이 데이터베이스에서 모든 데이터를 가져 오는 프로세스는 내가 엉망진창 일뿐입니다. 인터넷에서 몇 가지 자습서를 검색했고 매우 유용한 것을 발견했습니다 here. 나는 그것을 단계적으로 검토해 보았고, 그것은 의미가 있었으며, 모두 의미가 있었다. 그러나 그가 제공 한 db를 사용하는 대신, 내 자신의 db를 삽입하려고했습니다.이 모든 내용의 id는 볼 수 있어야하고 작동하지 않습니다. 그 좋은 던지기 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString' 내가 알아낼 수없는 오류. 그게 익숙한 소리로 들리겠습니까? mthe readFromDB 방법에 대한 코드는 다음과 같습니다sqlite db에서 여러 레코드 가져 오기 iphone sdk

-(void) readTextFromDatabase { 
// Setup the database object 
sqlite3 *database; 

// Init the animals Array 
textAr = [[NSMutableArray alloc] init]; 

// Open the database from the users filessytem 
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
    // Setup the SQL Statement and compile it for faster access 
    const char *sqlStatement = "select * from books";//This is where I'm running into trouble 
    sqlite3_stmt *compiledStatement; 
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
     // Loop through the results and add them to the feeds array 
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
      // Read the data from the result row 
      NSString *aChapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
      NSString *aSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 


      // Create a new text object with the data from the database 
      Text *text = [[Text alloc] initWithChapter:aChapter Section:aSection]; 

      // Add the text object to the text Array 
      [textAr addObject:text]; 

      [text release]; 
     } 
    } 
    // Release the compiled statement from memory 
    sqlite3_finalize(compiledStatement); 

} 
sqlite3_close(database); 

일부 변수의 이름을 전환 외에, 나는 아마 몇 가지 문제를 일으키는 언급 된 장소를 제외하고 그 코드의 어떤 부분을 변경되지 않았습니다. "animals에서 select *"문을 "animals from old table"이라는 이름의 "select from * books"문구로 변경하고 books는 새로운 db에서 사용할 테이블의 이름입니다. 너무 큰 거래는 아니 겠지만 분명히 그렇습니다. 나는 이것이 매우 깔끔한 질문이 아니라는 것을 알고 있습니다. 그러나 만약 내가이 벽에 나를 도울 수있는 누군가가 있다면, 그것은 대단히 감사 할 것입니다. 감사!!!

답변

1

sqlite를 사용할 때 같은 문제가 발생했습니다. null 문자열이 올 때 (어쩌면 행의 NULL 값에서오고있을 때) 정확하게 호출 할 수는 없지만이 코드를 사용하여 문제를 단계별로 처리했습니다.

+ (NSString*)stringWithCharsIfNotNull: (char*)chars 
{ 
    if (chars == NULL) 
     return nil; 
    else 
     return [[NSString stringWithUTF8String: chars] 
       stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 
관련 문제