0

I need to check my database column is not equal to null. I used one condition for 0 value like isEqual:@"0". How to use instead of isEqual use !=

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];

Insert:

-(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"];
        }
user3351727
  • 67
  • 4
  • 14

1 Answers1

1

Break up your code (and get rid of the extra square brackets):

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

Update:

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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Now it's a completely different question. Is your goal to check if the database value was `NULL`? If so, then you need to have code like in my answer. The call to `sqlite3_column_text` will return `NULL` if the column has a `NULL` value. You do not get a string whose value is `@"NULL"`. – rmaddy Mar 03 '14 at 06:58
  • My need is 4th column should be 0 and the 13th or 14th column not equal to null. Then i need to insert value...see my edited code. – user3351727 Mar 03 '14 at 07:02
  • The code in my answer solves the exception you are getting. You can't create the `NSString` if `sqlite3_column_text` is `NULL`. – rmaddy Mar 03 '14 at 07:03
  • My code was working with first condition isEqual:@"0". But now my requirement changed. So, i need to check 13th and 14th column – user3351727 Mar 03 '14 at 07:07
  • How can i check 13th or 14th column any one is not equal to null then i need to insert otherwise i won't insert. But when i run your code, if both null it inserting. – user3351727 Mar 03 '14 at 07:19
  • My updated answer will not go into the `if` statement if any one of the values is NULL. They all have to be non-NULL to enter the `if`. If only one needs to be non-NULL, adjust the expression as needed. – rmaddy Mar 03 '14 at 07:23
  • Table I'm getting NULL value insert: QM_ID QM_QRM_ID QM_LCM_ID QM_QCM_ID AM_Answer AM_Comments 432 1 5 1 NULL NULL – user3351727 Mar 03 '14 at 07:23
  • shall i use below: if ((answer || comments) && question && strcmp(question, "0") == 0) { // do your stuff here } – user3351727 Mar 03 '14 at 07:24
  • answer & comments is based on 13th and 14th column. Can you check my edited code.. – user3351727 Mar 03 '14 at 08:57