Search code examples
iosobjective-cdbaccess

Fetch query with null parameter in dbaccess


If I'hv two dbobject like:

@interface Member : DBObject

@property (strong) NSString* firstname;
@property (strong) NSString* lastName;
@property (strong) Group* group;

@end

@interface Group : DBObject

@property (strong) NSString* groupName;
@property (strong) NSString* adminName;

- (DBResultSet*)members;

@end

In member object, I can retrieve member which relates to the group, but in member object, there are also many objects which are not contained group object. Then how can I fetch them?

I tried with this, but given empty DBResultSet.

[[[Member query] whereWithFormat:@"group == %@",NULL] fetch];

Thank in adv.


Solution

  • Yes in SQLite you cannot equate NULL, so you use a slightly different expression to work with NULL objects. You need to use IS NULL or IS NOT NULL.

    So in short, your query above would become:

    [[[Member query] where:@"group IS NULL"] fetch];
    

    It does mess up building queries with parameters sometimes, but you just have to build up your query to look a little different in that case.

    For example:

    [[[Member query] whereWithFormat:@"(group == %@ OR group IS NULL)",@(123)] fetch];
    

    Thanks Ad