2013-05-18 2 views
0

sqlite DB를 가진 하나의 응용 프로그램을 원합니다. 내 sqlite DB에 5 레코드가 모든 레코드는 2 열이 존재합니다. (이름, ID, 키) 하나의 ID 중 하나가 NULL이고 이것을 얻고 싶습니다. (ID는 INTEGER 변수 임)xcode에서 sqlite로부터 NULL을 얻는 방법

이것은 내 코드이지만 실행하면 응용 프로그램이 중단됩니다.

do 
     { 
      sqlite3 *database2; 
      if(sqlite3_open([[self dataFilePath] UTF8String], &database2) == SQLITE_OK) 
      {    
       NSString *sqlStatement_userInfo2 =[NSString stringWithFormat:@"Select * from table1 where Name = %@ and ID = %@",p,p2]; 
       sqlite3_stmt *compiledStatement2; 
       if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo2 UTF8String], -1, &compiledStatement2, NULL) == SQLITE_OK) 
       { 
       // Loop through the results and add them to the feeds array 
        while(sqlite3_step(compiledStatement2) == SQLITE_ROW) 
        { 
         NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init]; 
         // Init the Data Dictionary 
         childID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 1)]; 
         childName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 0)]; 
         b = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 2) ]; 
         p = childID; 
         p2 = b; 
         [_dataDictionary setObject:[NSString stringWithFormat:@"%@",childName] forKey:@"Name"]; 
         [array addObject:_dataDictionary]; 
        } 
       } 

      else 
      { 
       NSLog(@"No Data Found"); 
      } 

       // Release the compiled statement from memory 
       sqlite3_finalize(compiledStatement2); 

      } 
       sqlite3_close(database2); 

     } while (b != NULL); 

이 코드가 작동하지 난이 오류 :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString' 
*** First throw call stack: 
(0x2092012 0x119fe7e 0x2091deb 0xb97480 0x35b7 0x2e08 0x1c8817 0x1c8882 0x1c8b2a 0x1dfef5 0x1dffdb 0x1e0286 0x1e0381 0x1e0eab 0x1e0fc9 0x1e1055 0x2e63ab 0x13792d 0x11b36b0 0x268efc0 0x268333c 0x268eeaf 0x1d68cd 0x11f1a6 0x11dcbf 0x11dbd9 0x11ce34 0x11cc6e 0x11da29 0x120922 0x1cafec 0x117bc4 0x117dbf 0x117f55 0x120f67 0xe4fcc 0xe5fab 0xf7315 0xf824b 0xe9cf8 0x1feddf9 0x1fedad0 0x2007bf5 0x2007962 0x2038bb6 0x2037f44 0x2037e1b 0xe57da 0xe765c 0x2b6d 0x2a95) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

답변

2

당신은 당신이 가진 NSString를 작성하기 전에 sqlite3_column_text(compiledStatement2, 1)NULL 바이트를 반환하는지 여부에로 수표를 추가해야합니다.

-1

ID는 정수는이를 사용하는 경우 :

if([ NSString stringWithFormat:@"%@", sqlite3_column_int(compiledStatement2, 1)]==nil) 
{ 
    childID = @"0"; 
    } 
    else 
    { 
    childID = [ NSString stringWithFormat:@"%@", sqlite3_column_int(compiledStatement2, 1)]; 
    } 
+0

childID가있는 NSString됩니다 – fred

+0

[있는 NSString stringWithFormat : "%의 @", sqlite3_column_int (compiledStatement2, 1) @] 문제는 sqlite3_column_text' '에 관한이 –

+0

으로 시도 'sqlite3_column_int'가 아닙니다. – rmaddy

관련 문제