我试图通过一个主要的细节表来迭代,我想在我去的时候填充主/细节结构。显然,当我嵌套结果集时,我得到一个BAD Access异常:
FMDatabase *db = self.database;
[db open];
db.traceExecution = YES;
db.logsErrors = YES;
FMResultSet *rs = [db executeQuery:@"select group_id, label from main.preference_group order by group_id"];
while ([rs next])
{
PreferenceGroup *pg = [[PreferenceGroup alloc] init];
pg.group_id = [rs intForColumn:@"group_id"];
pg.label = [rs stringForColumn:@"label"];
pg.translatedLabel = NSLocalizedString(pg.label, nil);
NSMutableArray * prefs = [[NSMutableArray alloc] init];
[prefGroups addObject:prefs];
FMResultSet *rs2 = [db executeQuery:@"select pref_id, label, value from main.preference where group_id = ? order by pref_id", pg.group_id, nil];
while ([rs2 next])
{
Preference * pref = [[Preference alloc] init];
pref.group_id = pg.group_id;
pref.pref_id = [rs2 intForColumn:@"pref_id"];
pref.label = [rs2 stringForColumn:@"label"];
pref.value = [rs2 stringForColumn:@"value"];
pref.translatedLabel = NSLocalizedString(pref.value, nil);
[prefs addObject:pref];
}
[rs2 close];
}
[rs close];
[db close];
在rs2(第二个结果集)中,我在FMDatabase类中获得了EXEC_BAD_ACCESS。
这是对sqlite3 / fmdb的限制还是我在这里做错了什么?
答案 0 :(得分:2)
我刚刚发现我做错了什么。我正在传递一个int作为第二个查询的一部分。我不得不将其转换为NSNumber:
FMResultSet *rs2 = [db executeQuery:@"select pref_id, label, value from main.preference where group_id = ? order by pref_id", [NSNumber numberWithInt:pg.group_id], nil];
这意味着,YES,sqlite3 / fmdb确实支持嵌套查询! : - )
答案 1 :(得分:0)
我也在使用FMDB和SQLITE3,我发现嵌套查询有效:
(我没有声称下面的代码提示是好的,请不要介意格式,请
Master和Detail表都有一个名为'id'的列
FMResultSet *rso = [database executeQuery:@"select * from master order by id"];
while ([rso next])
{
NSInteger masterId = [rso intForColumn:@"id"];
NSString *q3 = [[NSString alloc] initWithFormat:
@"select * from detail where masterid = %d order by id", masterId, nil ];
FMResultSet *rsa = [database executeQuery:q3 ];
while ([rsa next])
{
NSInteger detailId = [rsa intForColumn:@"id"];
//
// here do something with masterId and detailId
}
}
实际上,这是一个令人惊喜的惊喜。我半途而废,希望首先查询所有主记录,然后在App内存中循环访问它们以从SQlite3中获取详细信息。
但上面的结构工作正常。