amit srivastava
amit srivastava

Reputation: 77

How to retrieve the data from sqlite and save in array using FMDB

I have successfully save the data in sqlite db but I am unable to save this data in array.

- (void) createEventTable {

[self createAndDropTablesWithQuery:[NSString stringWithFormat:@"create table if not exists BEACONDATA (rowID integer primary key AUTOINCREMENT, beaconid text)"]];

}

-(void)insertData:(NSArray *)beacon_data
{
    [self createEventTable];
    
    [instance.database open];
    BOOL isInserted;
    @autoreleasepool
    {
        isInserted=[instance.database executeUpdate:@"insert into BEACONDATA (beaconid) values (?)",[NSString stringWithFormat:@"(%@)",beacon_data]];
    } 

[instance.database close];

if(isInserted)
    NSLog(@"Inserted Successfully");
else
    NSLog(@"Error occurred while inserting");

[self displayData];
 }

 -(void)displayData
{

[instance.database open];


FMResultSet *resultSet=[instance.database executeQuery:@"SELECT * FROM BEACONDATA"];
NSMutableArray *array_lastname=[[NSMutableArray alloc]init];

if(resultSet)
{
    while([resultSet next])
        
     NSLog(@"Beacon data %@",[resultSet stringForColumn:@"beaconid"]);

}

}

OUTPUT:

Beacon data (01000444)
Beacon data (01000466)
Beacon data (01000001)
Beacon data (01000468)
Beacon data (01000004)
Beacon data (01000006)
Beacon data (01000003)

How to save this data in array I have tried a lot of way but not success please help if you have any idea. I am new in sqlite.

Upvotes: 0

Views: 322

Answers (1)

Nirav D
Nirav D

Reputation: 72410

FMResultSet have instance method resultDictionary will give you NSDictionary for that records. So you can add object of that dictionary in your array.

NSMutableArray *array = [[NSMutableArray alloc]init];
if(resultSet) {
while([resultSet next]) {
    NSDictionary *dict = [resultSet resultDictionary];
    [array addObject:dict];

    //Or if you want to add simply `beaconid` to array then it should be simply
    [array addObject: [resultSet stringForColumn:@"beaconid"]];
}

Upvotes: 1

Related Questions