2014-03-03 19 views
0

我需要檢查我的數據庫列是否等於null。我使用了一個條件,如isEqual的0值:@「0」。如何使用替代的isEqual使用=如何使用!= NULL

的SQLStatement:

sqlStatement =[[NSString stringWithFormat:@"SELECT a.*,b.AM_Answer,b.AM_Comments 
FROM question_master a 
left join Assessment_master b 
on (b.AM_QM_ID = a.QM_ID AND b.AM_HNM_ID = %d AND b.AM_HM_ID = %d and b.AM_ASM_Local_Id = %@) where a.QM_Parent_Id = 0 and a.QM_Status ='A' and a.QM_QCM_ID =%@ and a.QM_QRM_Id = %@ union SELECT c.*,d.AM_Answer,d.AM_Comments FROM question_master c left join Assessment_master d on (d.AM_QM_ID = c.QM_ID AND d.AM_HNM_ID = %d AND d.AM_HM_ID = %d and d.AM_ASM_Local_Id = %@) where c.QM_Parent_Id in (SELECT QM_ID FROM question_master where QM_Parent_Id = 0 and QM_Status ='A' and QM_QCM_ID =%@ and QM_QRM_Id = %@) and c.QM_Status ='A' and c.QM_QCM_ID =%@ and c.QM_QRM_Id = %@" 

    return [self getOuestionDetails_Answer:sqlStatement]; 

插入:

-(NSMutableArray*)getOuestionDetails_Answer:(const char*)sqlStatement{ 
    sqlite3_stmt *compiledStatement; 

    NSMutableArray* parentArray = [NSMutableArray new]; 

    int result = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) ; 
    if(result == SQLITE_OK) 
    { 
     NSMutableDictionary* parentDict = [NSMutableDictionary new]; 

     NSMutableArray* childArray = [NSMutableArray new]; 
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
     char *question = (char *)sqlite3_column_text(compiledStatement, 4); 
      char *answer = (char *)sqlite3_column_text(compiledStatement, 13); 
      char *comments = (char *)sqlite3_column_text(compiledStatement, 14); 

        if (answer && comments && question && strcmp(question, "0") == 0) { 

    [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)] forKey:@"QM_ID"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)] forKey:@"QM_QRM_ID"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] forKey:@"QM_LCM_ID"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)] forKey:@"QM_QCM_Id"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)] forKey:@"QM_Question"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)] forKey:@"QM_Question_Parent"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)] forKey:@"QM_Type"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)] forKey:@"QM_Parent_Id"]; 

        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)] forKey:@"QM_Status"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)] forKey:@"QM_Created_Date"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)] forKey:@"QM_Created_By"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)] forKey:@"QM_Modified_Date"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 12)] forKey:@"QM_Modified_By"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 13)== NULL?"":(char *)sqlite3_column_text(compiledStatement, 13)] forKey:@"AM_Answer"]; 
        [parentDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 14)== NULL?"":(char *)sqlite3_column_text(compiledStatement, 14)] forKey:@"AM_Comments"]; 
     } 

回答

1

分手你的代碼(和擺脫多餘的方括號中):

char *dbStr = (char *)sqlite3_column_text(compiledStatement, 4); 
if (dbStr) { 
    NSString *str = [NSString stringWithUTF8String:dbStr]; 
    // do stuff with str 
} else { 
    // database value was NULL 
} 

更新:

char *question = (char *)sqlite3_column_text(compiledStatement, 4); 
char *answer = (char *)sqlite3_column_text(compiledStatement, 13); 
char *comments = (char *)sqlite3_column_text(compiledStatement, 14); 
if (answer && comments && question && strcmp(question, "0") == 0) { 
    // do your stuff here 
} 
+0

檢查我編輯的代碼。我必須檢查這些值 – user3351727

+0

現在這是一個完全不同的問題。您的目標是檢查數據庫值是否爲「NULL」?如果是這樣,那麼你需要有我的答案中的代碼。如果列的值爲NULL,那麼對sqlite3_column_text的調用將返回NULL。你不會得到一個字符串,它的值是'@「NULL」'。 – rmaddy

+0

我的需求是第4列應該是0和第13或第14列不等於null。然後我需要插入值...看我編輯的代碼。 – user3351727