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-04-2010, 07:49 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default Sqlite not returning integers, help!

Hi there!

I'm working in a project that uses Sqlite3. I've made a singleton database class so I need to use it from nearly everywhere in my project and I've added some methods to this singleton class so I can get information of a given table, row and identifier. I've made two different methods: one of them returns a NSString* and one of them returns an int (because I've only these two types of data in my DB). There is no problem when getting the NSString and even when getting the whole table information (NSString and integers), however if I try to get a single int it always returns 0 though I use the same method that I use to get the NSStrings:
Code:
// It always gives me the correct NSString
-(NSString*)getStringFromTable:(NSString*)table Row:(NSString*)row andIdent:(int)ident
{
	NSString *sql;
	
	sql = [NSString stringWithFormat:@"SELECT %@.%@ FROM %@ WHERE %@.id = %d;",table,row,table,table,ident];		
	
	sqlite3_stmt *selectstmt;
	if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) {
		
		if (sqlite3_step(selectstmt) == SQLITE_ROW) {
			return [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
		}else {
			return @"NULL";
		}
	}else {
		return @"NULL";
	}
}

// It always returns 0 (though database integers are different to 0 (they goes from 1 to 9)
-(int)getIntFromTable:(NSString*)table Row:(NSString*)row andIdent:(int)ident
{
	sqlite3_stmt *selectstmt;
	
	NSString *sql = [NSString stringWithFormat:@"SELECT %@.%@ FROM %@ WHERE %@.id = %d;",table,row,table,table,ident];	

	if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) {

		return sqlite3_column_int(selectstmt, 0);
		
	}else {
		return -1;
	}
}

// And this method prints a whole row, and returns all the information correctly
-(void)printData
{
	sqlite3_stmt *selectstmt;
	
	NSString *sql = @"SELECT * FROM myTable;";
	
	//selectstmt;
	if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectstmt, NULL) == SQLITE_OK) {
		
		while(sqlite3_step(selectstmt) == SQLITE_ROW) {
			// Aquest registre donara tots els identificadors dels objectes
			
			NSLog(@"ID:%d",sqlite3_column_int(selectstmt, 0));
			NSLog(@"%@ --- %@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)],[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]);
			NSLog(@"Type:%d",sqlite3_column_int(selectstmt, 3));
			NSLog(@"Parameters:%d,%d,%d,%d,%d,%d",sqlite3_column_int(selectstmt, 4),sqlite3_column_int(selectstmt, 5),sqlite3_column_int(selectstmt, 6),sqlite3_column_int(selectstmt, 7),sqlite3_column_int(selectstmt, 8),sqlite3_column_int(selectstmt, 9));
		}
	}
}
Does anybody knows what can be happening? It seems to have no sense so it's nearly the same query and if I log the query's result in the getInt method (just before returning it) it always returns 0...

Thanks in advance!
Johanovski is offline   Reply With Quote
Old 06-04-2010, 03:12 PM   #2 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

I may be mistaken but aren't you missing the step function. Your if is the prepare statement. I'm pretty sure you need the step like you have for the nsstring function
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 06-07-2010, 01:46 AM   #3 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default

Ouch, you're right! XD Sorry, too much hours working...!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Tags
database, selectstmt, sqlite3, sqlite3_column_int, 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: 354
12 members and 342 guests
ajay123123, Anwerbl, Arty Tales, ashaman64, baja_yu, ChrisYates, HemiMG, mini998, mottdog, newDev, Objective Zero, oceanlablight
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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