Convert FMResultSet to NSMutableArray?

I tried FMDB and it looks like it will work fine for me if I can get my FMResultSet to turn into NSMutableArray.

How to do it?

+4
source share
2 answers

The sample code for FMDB pretty clearly shows how FMResultSet works. You iterate over the rows in the result set by calling the next method at each iteration. Inside the loop, you use data access methods to retrieve the data in the column for the current row. If you want to turn this into an array, you must do it manually. Like this:

 NSMutableArray *array = [NSMutableArray array]; FMResultSet *rs = [db executeQuery:@"select * from table"]; while ([rs next]) { // Get the column data for this record and put it into a custom Record object int col1 = [rs intForColumn:@"col1"]; int col2 = [rs intForColumn:@"col2"]; Record *record = [Record recordWithCol1:col1 col2:col2]; [array addObject:record]; } [rs close]; 

As you can see, I assume that you created your own Record class, which represents an entry in your database table. Of course, you can also work with the dictionary.

+9
source

You can try this.

 NSMutableArray *array = [NSMutableArray array]; FMDatabase *database = [FMDatabase databaseWithPath:databasePath]; [database open]; FMResultSet *results = [database executeQuery:@"SELECT * FROM Table"]; while ([results next]) { [array addObject:[results resultDictionary]]; } NSLog(@"%@", array); [database close]; 

I use this in a project and it works great.

+13
source

Source: https://habr.com/ru/post/1385578/


All Articles