views:

144

answers:

1

Hi,

when selecting a column that can contains a NULL value I get an exception thrown on this statement.

self.DirectionFromCity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];

How should I go about setting these attributes as NULL values are OK for the Database.

A: 

As per docs NSString throws an exception if the bytes are null

What you need to do is check the returned value and only create the string if it is not null

char *localityChars = sqlite3_column_text(compiledStatement, 8);

if (localityChars == nil) {
     self.DirectionFromCity = nil;
} else {
     self.DirectionFromCity = [NSString stringWithUTF8String: localityChars];
}

In response to the comment below re code growing with the extra checking, you can shrink it down using

self.DirectionFromCity = localityChars != nil?[NSString stringWithUTF8String: localityChars]:nil;

or push it out to a method that will do the same functionality

self.DirectionFromCity = [self getSQLiteColumn:compiledStatement];
Liam
Thanks,Ouch... I have 35 columns that can contain NULL this will add a lot of overhead to the app......
MB
The overhead will be quite small, the only real difference is the 'if' block
Liam
Liam: I think he meant the overhead to the code-base.
My Other Me
Thanks Again, I am concerned about both app performance and the size of the code neat solutions
MB