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 06-10-2010, 11:56 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 6
sarahhbabyyyy is on a distinguished road
Default Querying two variables in NSUserDefaults with stringWithFormat

Hey Everyone,

So, I've been stuck on this all morning. I am trying to query two variables that I saved in NSUserDefaults and it doesn't seem to work. When I change the variables to regular NSStrings instead of NSUserDefaults, it works... Can I not do the following with NSUserDefaults? Or is there another way? Please help!

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *brandName = [defaults stringForKey:@"selectedBrand"];
NSString *modelName = [defaults stringForKey:@"selectedModel"];
NSString *sqlStr = [NSString stringWithFormat:@"select * from vehicleInfo where brandName = %@ and modelName = %@", brandName, modelName];

Sarah
sarahhbabyyyy is offline   Reply With Quote
Old 06-10-2010, 12:12 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sarahhbabyyyy View Post
Hey Everyone,

So, I've been stuck on this all morning. I am trying to query two variables that I saved in NSUserDefaults and it doesn't seem to work. When I change the variables to regular NSStrings instead of NSUserDefaults, it works... Can I not do the following with NSUserDefaults? Or is there another way? Please help!

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *brandName = [defaults stringForKey:@"selectedBrand"];
NSString *modelName = [defaults stringForKey:@"selectedModel"];
NSString *sqlStr = [NSString stringWithFormat:@"select * from vehicleInfo where brandName = %@ and modelName = %@", brandName, modelName];

Sarah

Your code looks like it should work as written.

What values are you getting for brandName and ModelName? Are those pointers nil? If not nil, do they contain empty strings?

Are you confident that those keys contain valid string data? Are you positive that the keys are correct, including case? It's a good habit to get into to use #defined constants for keys. That way there is no chance to have a mix-up or typo in a key.

Have you tried writing known values to those keys in userDefaults and doing a [userDefaults synchronize] call to force them to be flushed to disk right before the code above?


Regards,

Duncan C
WareTo
Check out our apps in the Apple App store
Duncan C is offline   Reply With Quote
Old 06-10-2010, 12:52 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 6
sarahhbabyyyy is on a distinguished road
Default

Hi Duncan,

Thanks for your response. I am calling the same userDefaults somewhere else in my project and it works fine. I am querying the same thing somewhere else and it seems to be working there.

Let me show you more of my code... I took a variable out of my query and just sticking to one for now...maybe I am doing something wrong:

sqlite3 *database;

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *brandName = [defaults stringForKey:@"selectedBrand"];
NSString *sqlStr = [NSString stringWithFormat:@"select * from vehicleInfo where brandName = '%@'", brandName];

const char *sqlStatement = [sqlStr UTF8String];

sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row in column 2
NSString *aName = [NSString stringWithUTF8Stringchar *)sqlite3_column_text(compiledStatement, 2)];


[defaults setObject:aName forKey:@"selectedSpur"];

}
}

//Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);
sarahhbabyyyy is offline   Reply With Quote
Old 06-10-2010, 01:16 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 6
sarahhbabyyyy is on a distinguished road
Default

Nevermind! I figured out my problem... I was calling the key before it was actually saved into NSUserDefaults... whatta noob.. thanks!
sarahhbabyyyy is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults, query, stringwithformat

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: 336
11 members and 325 guests
Absentia, Domele, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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