Sqlite3 and fmdb nested FMResultSet possible?

I am trying to execute an iterator through a master type table, and I would like to populate the master / detail structure when I go. Apparently, when I install the result sets, I get a BAD Access exception:

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];

In rs2 (second result set) I get EXEC_BAD_ACCESS in class FMDatabase.

Is this a limitation of sqlite3 / fmdb or am I doing something wrong here?

+5
source share
2 answers

I just found that I did wrong. I passed int as part of the second request. I had to convert it to 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];

So that means yes, sqlite3 / fmdb supports nested queries! :-)

+2
source

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

0

All Articles