Ok, so I’m getting better with SQLite queries … but obviously not a master yet.
I am trying to loop through the array this returns, and all I am getting is 0 for every entry.
Here is what is making the query call and returning the result (this particular query should currently return 6 lines of data):
-(NSMutableArray *)getMyAthleteList
{
NSMutableArray *athleteList = [[NSMutableArray alloc] init];
NSString *getAthleteListSQL = [[NSString alloc] initWithFormat:@"SELECT * FROM athletes"];
sqlite3_stmt *selectStmt;
if (sqlite3_prepare_v2(database, [getAthleteListSQL UTF8String], -1, &selectStmt, nil)==SQLITE_OK)
{
while (sqlite3_step(selectStmt)==SQLITE_ROW)
{
//NSLog(@"Adding athletes to the array");
Athletes *athlete_array = [Athletes alloc];
athlete_array.athlete_image = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 0)];
athlete_array.athlete_id = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 1)];
athlete_array.athlete_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 2)];
athlete_array.school_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 3)];
athlete_array.sport_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 4)];
athlete_array.athlete_flag = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 5)];
[athleteList addObject:athlete_array];
}
}
else
{
NSLog(@"Did not make a good query");
}
return athleteList;
}
Now here is what I am doing when calling and looping through it:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *myAthleteList = [[NSArray alloc] initWithArray:[appDelegate getMyAthleteList]];
for (Athletes *athlete in myAthleteList) {
NSLog(@"Athlete Name: %@", athlete.athlete_name);
NSLog(@"Athlete ID: %@", athlete.athlete_id);
}
And the output is:
Athlete Name: 0
Athlete ID: 0
My strings (athlete_name, athlete_id, etc.) are all defined correctly (in my Athletes.m and .h files).
I’m sure I’m missing something simple, but any help would be highly appreciated.
Thanks!
Are you expecting lines like this to return a string from the DB?
If so, you might want to look into sqlite3_column_text or text16
See: http://www.sqlite.org/capi3ref.html#sqlite3_column_blob
You also may want to use this since it returns a const char *:
For example, here’s a similar snippet from one of my samples:
Also, look into saving off your compiled statement &selectStmt and calling sqlite3_reset to use again. It’s compiled into byte code at that point and you don’t need to parse and compile your sql statement over and over.
See: http://www.sqlite.org/capi3ref.html#sqlite3_prepare
When you’re done with the statement (completely if you save it off), don’t forget to call sqlite3_finalize.