Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-15-2009, 08:00 PM   #1 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 4
HAiRYANiMAL is on a distinguished road
Question NSString, sqlite3_column_text NULL problem?

Hello all!

In my app I want to support the sqlite3 DB having NULL values in the DB for the items that should be NULL (i.e. non-edited data).

So I have my sqlite3 DB all created and filled with data, my app loads beautifully and there aren't any problems. For the varchar fields I want to be NULL I have currently put in a ' ' (space).

I am now at a point where I need to fix my data in the DB to really be NULL if it should be empty (instead of the ' ' (space) placeholder I have put in).

So I have modified my sqlite3 DB data with Firefox SQLite Manager and reset my iPhone Simulator. The app launches, copies the new DB as it doesn't exist... But I don't know how to handle the NULL when I read in from the DB. I do a:
Code:
self.Locality = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 12)];
And I get an uncaught exception:
Code:
2009-01-15 18:28:38.987 iApp[1170:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
I understand that the column I have has NULL data - that is what I want. But I do not know how to pre-determine that it is NULL so I can just set self.Locality to nil.

Any help would be great, thanks
HAiRYANiMAL is offline   Reply With Quote
Old 01-15-2009, 08:41 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I think checking the pointer you got from sqliite against null should do it. Something like this (Not tested!) :

Code:
char *localityChars = sqlite3_column_text(init_statement, 12);

if (localityChars ==null)
     self.Locality = nil;
else
     self.Locality = [NSString stringWithUTF8String: localityChars];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-18-2009, 03:16 PM   #3 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 4
HAiRYANiMAL is on a distinguished road
Default

Yes, that worked. Thank you.
HAiRYANiMAL is offline   Reply With Quote
Old 08-13-2010, 07:20 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi,

I have exactly the same issue and have been playing with smasher's code without success.

Code:
NSString *aCurrencyCoin = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 44)] autorelease];
So I change the code to:
Code:
char *aCurrencyCoin = (char *)sqlite3_column_text(compiledStatement, 44); //Conflicting types for "aCurrencyCoin"
if (aCurrencyCoin ==null) // null undeclared
   aCurrencyCoin = nil;
else
   aCurrencyCoin = [NSString stringWithUTF8String: aCurrencyCoin];

Country *country = [[Country alloc] initWithName:aName CurrencyCoin:aCurrencyCoin ]; // there is a lot more added to the array from earlier code.
I get the above errors:

Can anyone see where I am doing this wrong.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-13-2010, 09:30 AM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The function sqlite3_column_text returns a char pointer; is Country initWithName:CurrencyCoin: expecting a NSString? They're two different types. The 'Conflicting types' is probably coming up if you declare "aCurrencyCoin" as two different type, possibly one in the .h and on in the method?

Try this, it may make it more obvious:
Code:
char *currencyChars = (char *)sqlite3_column_text(compiledStatement, 44);

NSString *currencyString = nil;
if (currencyChars ==NULL)
   currencyString = [NSString stringWithUTF8String: aCurrencyCoin];

Country *country = [[Country alloc] initWithName:aName CurrencyCoin:currencyString ];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 08-13-2010, 10:02 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2010
Location: Warwickshire, United Kingdom
Posts: 163
dcjones is on a distinguished road
Default

Hi,

Still getting errors. The code below is a portion of the complete code.
In the .h I have "NSString *CurrencyCoin;" and "@property (nonatomic, retain) NSString *CurrencyCoin;".

The error now is"passing argument 1 of 'stringWithUTF8String:' from incompatible pointer type"


Code:
NSString *aCapCityLat = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 42)] autorelease];
NSString *aCapCityLng = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 43)] autorelease];
NSString *aCurrencyCoin = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 44)] autorelease];
			
char *currencyChars = (char *)sqlite3_column_text(compiledStatement, 44);
			
NSString *currencyString = nil;
	if (currencyChars ==NULL)
	currencyString = [NSString stringWithUTF8String: aCurrencyCoin];// ERROR HERE
			
NSString *aCurrencyNote = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 45)] autorelease];
NSString *aCurrencyPic = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 46)] autorelease];
NSString *aDrive = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 47)] autorelease];
After this code has run all the content of the NSString's, example " *CurrencyCoin" get written to an Array for later use.

Can you see what is wrong.

Again, thanks for the time you have given.
__________________
Many thanks, keep safe and well.



Dereck
dcjones is offline   Reply With Quote
Old 08-14-2010, 01:13 AM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The string you get back from "stringWithUTF8String" is already autoreleased - you should not call autorelease on it again, you'll cause a crash.

If you want to use CurrencyCoin instead of currencyString that's fine; variables usually get lowercase names though.

Code:
NSString *aCapCityLat = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 42)];
NSString *aCapCityLng = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 43)];
			
char *currencyChars = (char *)sqlite3_column_text(compiledStatement, 44);
			
CurrencyCoin = nil;
if (currencyChars !=NULL)
	CurrencyCoin = [NSString stringWithUTF8String: currencyChars];
			
NSString *aCurrencyNote = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 45)];
NSString *aCurrencyPic = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 46)];
NSString *aDrive = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 47)];
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
cstring, nil, nsstring, null, sqlite3_column_text

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 415
17 members and 398 guests
Alex-alex, Apptronics RBC, baja_yu, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0