好的,所以我的SQLite查询越来越好......但显然还不是主人。
我试图遍历这个返回的数组,而我得到的每个条目都是0。
以下是进行查询调用并返回结果的内容(此特定查询当前应返回6行数据):
-(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;
}
现在这是我在调用和循环时所做的事情:
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);
}
输出是:
运动员姓名:0 运动员ID:0
我的字符串(athlete_name,athlete_id等)都已正确定义(在我的Athletes.m和.h文件中)。
我确信我错过了一些简单的东西,但任何帮助都会受到高度赞赏。
谢谢!
答案 0 :(得分:1)
您是否希望这样的行从DB返回一个字符串?
athlete_array.athlete_name = [[NSString alloc] initWithFormat:@"%d",
sqlite3_column_int(selectStmt, 2)];
如果是这样,您可能需要查看sqlite3_column_text或text16
请参阅:http://www.sqlite.org/capi3ref.html#sqlite3_column_blob
您也可能想要使用它,因为它返回一个const char *:
[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(...)
例如,这是我的一个样本中的类似片段:
contact = [[Contact alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW)
{
int idx = 0;
NSNumber *idField = [NSNumber numberWithLongLong:sqlite3_column_int64(statement, idx++)];
[contact setId:idField];
NSString *nameField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, idx++)];
[contact setName:nameField];
NSString *addressField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, idx++)];
[contact setAddress:addressField];
NSString *phoneField = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, idx)];
[contact setPhone:phoneField];
NSLog(@"id: %@", [contact id]);
NSLog(@"name: %@", [contact name]);
NSLog(@"address: %@", [contact address]);
NSLog(@"phone: %@", [contact phone]);
// Code to do something with extracted data here
[nameField release];
[phoneField release];
[addressField release];
}
另外,请考虑保存已编译的语句& selectStmt并调用sqlite3_reset再次使用。它在那时编译成字节代码,你不需要一遍又一遍地解析和编译你的sql语句。
请参阅:http://www.sqlite.org/capi3ref.html#sqlite3_prepare
完成语句后(完全如果你将其保存),不要忘记调用sqlite3_finalize。