0

The following code :

-(NSArray *)getContentsWithContextTypes:(NSArray *)contextTypes
                            contextData:(NSArray *)contextData
{
    __block NSString *query = @"SELECT * FROM Texts_original1 WHERE ";
    [contextData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *attributeName = [self.contextTypeToDBQueryTexts          
                                        objectForKey:contextTypes[idx]];
        query = [query stringByAppendingFormat:@"%@ = \"%@\"", attributeName, obj];
        if(idx != contextData.count - 1)
        {
            query = [query stringByAppendingString:@" AND "];
        }
    }];

    [self.db open];
    FMResultSet *results = [self.db executeQuery:query];
    NSMutableArray *array = [NSMutableArray array];

    while([results next])
    {
        Content *content = [[TextualContent alloc] initWithResults:results];
        [array addObject:content];
    }

    [self.db close];
    return array;
}

Generates the following error when I run it :

Error calling sqlite3_step (21: out of memory) rs

It happens half way through the loop. There should be 33 results. After 17 I get that error and the loop exits. Any Ideas? Thanks.

foFox
  • 1,124
  • 1
  • 14
  • 24

1 Answers1

6

It's important to know that you close the FMResultSet object, as it will cause memory problems. Do it as follows, [resultSet close]

I am not getting into your code logic but, I will make some modifications in database code.

Try the following:

-(NSArray *)getContentsWithContextTypes:(NSArray *)contextTypes
                        contextData:(NSArray *)contextData
{
 __block NSString *query = @"SELECT * FROM Texts_original1 WHERE ";
 [contextData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) 
   {
     NSString *attributeName = [self.contextTypeToDBQueryTexts          
                                    objectForKey:contextTypes[idx]];
     query = [query stringByAppendingFormat:@"%@ = \"%@\"", attributeName, obj];
     if(idx != contextData.count - 1)
     {
        query = [query stringByAppendingString:@" AND "];
     }
  }];

self.db = [FMDatabase databaseWithPath:[self getDBPath]];    

if(![db open])
{
  NSLog(@"Could not open DB");
  return nil;
} 

FMResultSet *results = [self.db executeQuery:query];
NSMutableArray *array = [NSMutableArray array];

while([results next])
{
    Content *content = [[TextualContent alloc] initWithResults:results];
    [array addObject:content];
}

[results close]; //VERY IMPORTANT!
[self.db close];
return array;

}

Ravi Raman
  • 1,070
  • 9
  • 16
  • Nice, I'll check it and have a try. – Autobots May 02 '13 at 02:28
  • Try www.github.com/pmurphyjam/DBExample It's an Xcode project that uses Sqlite. It doesn't have these memory issues, and can do SQL transactions for writing large queries. – Pat May 22 '14 at 21:33