我需要检查我的数据库列是否等于null。我使用一个条件为0值,如isEqual:@“0”。如何使用而不是isEqual使用!=
的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"];
}
答案 0 :(得分: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
}