3

I can read all column Data from code like this ...

FMResultSet *rs = [db executeQuery:@"SELECT Name, Age, FROM PersonList"];

while ([rs next]) {

NSString *name = [rs stringForColumn:@"Name"];

int age = [rs intForColumn:@"Age"];

}

or find some Data from this way

NSString *address = [db stringForQuery:@"SELECT Address FROM PersonList WHERE Name = ?",@"John"];

But if I want a Array contents the whole row's data(assume all my row data is a simple String)

How can I achieve that?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
bbbbbird1
  • 55
  • 2
  • 6

2 Answers2

7

There is a resultDict method defined on the FMResultSet class. I would do it like this:

FMResultSet *rs = [db executeQuery:@"SELECT Name, Age, FROM PersonList"];
while ([rs next]) {
    NSLog(@"%@", [[rs resultDict] description]);
}

This should print something like:

{
    Name = bbbbbird1;
    Age = 25;
}

for every row in the PersonList table. Now there are two ways of putting these values into the array. One is to use allValues method of NSDictionary, however the order of the columns will most probably be broken. The other way is to build the array yourself:

FMResultSet *rs = [db executeQuery:@"SELECT Name, Age, FROM PersonList"];
while ([rs next]) {
    NSMutableArray* rowData = [[NSMutableArray alloc] init];
    for(int i=0; i<[s columnCount]; i++) {
        [rowData addObject:[[rs resultDict] objectForKey:[[rs columnNameForIndex:i] lowercaseString]];
    }
    NSLog(@"%@", [rowData description]);
}

The above should print:

(
    bbbbbird1,
    25
)

I hope this is what you are looking for. You can put this code in the FMResultsSet category if you need to have the rows as arrays in many places in you app.

lawicko
  • 7,246
  • 3
  • 37
  • 49
3

The accepted answer is great but there is an elegant way to achieve the example the OP gave:

FMResultSet *rs = [db executeQuery:customQuery];
while ([rs next]) {
   NSString* tempName = [rs objectForColumnName:@"someColumnName"];
}

According to the objectForColumnName documentation the return value:

Either NSNumber, NSString, NSData, or NSNull. If the column was NULL, this returns [NSNull null] object.

So it can be achieved with a single method (assuming you know the type of your DB).

Also, if you are looking for a custom array that contains class objects you can combine them both:

FMResultSet *rs = [db executeQuery:customQuery];
while ([rs next]) {
   someCustomClassName *tempClass = [[someCustomClassName alloc] init];
   [tempClass setSomePropertyValue:[rs objectForColumnName:@"someColumnName"];
   [someArray addObject:tempClass]];
}
OhadM
  • 4,687
  • 1
  • 47
  • 57