2013-05-18 31 views
0

我想要一個具有sqlite數據庫的應用程序。 在我的sqlite數據庫中存在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)]嘗試與此 –

+0

的問題是關於'sqlite3_column_text' ,而不是'sqlite3_column_int'。 – rmaddy