0

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.

1 Answer 1

3

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];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Ouch... I have 35 columns that can contain NULL this will add a lot of overhead to the app......
The overhead will be quite small, the only real difference is the 'if' block
Thanks Again, I am concerned about both app performance and the size of the code neat solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.