如何从sqlite中重新保存数据并使用FMDB保存在数组中

时间:2022-06-01 21:36:21

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

我已成功将数据保存在sqlite db中,但我无法将此数据保存在数组中。

- (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 occured 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:

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.

如何在数组中保存这些数据我已经尝试了很多方法,但没有成功请帮助,如果你有任何想法。我是sqlite的新手。

1 个解决方案

#1


1  

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

FMResultSet有实例方法resultDictionary将为您提供该记录的NSDictionary。因此,您可以在数组中添加该字典的对象。

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

#1


1  

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

FMResultSet有实例方法resultDictionary将为您提供该记录的NSDictionary。因此,您可以在数组中添加该字典的对象。

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