2017-01-30 4 views
0

sqlite dbにデータを正常に保存しましたが、このデータを配列に保存できません。sqliteからデータを後退させ、FMDBを使用してアレイに保存する方法

- (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:私は道の多くを試してみましたが、あなたはどんな考えを持っている場合ではない成功を助けてください、アレイ内のこのデータを保存する方法

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

。私はsqliteで新しいです。

答えて

1

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

回答ありがとうNSDictionary * dict = [resultSet resultDictionary];この言葉は無用です。 –

+0

私はすでにこれを試している[resultSet stringForColumn:@ "beaconid"]も与えない。 –

+0

[resultSet stringForColumn:@ "beaconid"] && [resultSet resultDictionary]これら2つはns logで表示されますが、配列には保存されません。理解できません。 –

関連する問題