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...
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