使用FMDB获取复杂数据

时间:2015-06-14 22:46:35

标签: sql objective-c sqlite fmdb

我在iOS应用程序中使用SQLite,我使用FMDB作为包装器。这是我的数据库架构:

CREATE TABLE Offer (code TEXT PRIMARY KEY NOT NULL, name TEXT);

CREATE TABLE OffreMarket (codeOffer TEXT NOT NULL,
codeMarket TEXT NOT NULL,
FOREIGN KEY(codeOffer) REFERENCES Offer(code),
FOREIGN KEY(codeMarket) REFERENCES Market(code));

CREATE TABLE Market (code TEXT PRIMARY KEY NOT NULL, name TEXT);

我的模型对象:

@interface Offer : NSObject
@property (nonatomic,copy) NSString *code;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSArray *markets;
@end

@interface OffreMarket : NSObject
@property (nonatomic,copy) NSString *codeOffer;
@property (nonatomic,copy) NSString *codeMarket;
@end

@interface Market : NSObject
@property (nonatomic,copy) NSString *code;
@property (nonatomic,copy) NSString *name;
@end

例如,我正在获取数据库中的所有商品,如下所示:

- (NSArray *)offers {
// Open database

NSMutableArray *offers = [NSMutableArray new];

FMResultSet *resultSet = [database executeQuery:@"SELECT * FROM Offer"];
while ([resultSet next]){

   Offer *offer = [Offer new];
   offer.code = [resultSet stringForKey:@"code"];
   offer.name = [resultSet stringForKey:@"name"];

   // Get the markets for each offer
   FMResultSet *marketResultSet = [database executeQuery:@"SELECT * FROM  OffreMarket WHERE codeOffer = ?",offer.code];

   NSMutableArray *offers = [NSMutableArray new];
   while ([marketResultSet next]) {
      OffreMarket *offerMarket = [OffreMarket new];
      ....
     [offers addObject:offerMarket];
   }

  market.offers = [offers copy];
}

return [offers copy]

}

这是有效的,但需要时间,因为我使用了很多SQL请求来获取所有商品和相应的市场。

我可以避免许多SQL请求获取相应市场的所有优惠吗?谢谢你的回答

3 个答案:

答案 0 :(得分:5)

我可以建议:

  • 优化您的SQL语句。您只需将语句更改为" SELECT * FROM OffreMarket WHERE codeOffer IN (SELECT code FROM Offer)"而不是2个循环。如果您想使用列" name"在表格" Offer"中,您可以加入两个表格" codeOffer"和" Offer"。这里的经验法则是避免过多的循环,但尝试组合或优化您的SQL语句。

  • 为列" code"创建索引在表格中#34; Offer"。它会加速你的搜索速度。例如,在我的一个项目中,我不得不在具有36K记录的SQL表上工作。通过将简单索引设置为主键列,我设法将该表上的搜索时间缩短了10秒。

答案 1 :(得分:4)

在这种情况下,您只需一个查询即可获得结果:

select * from Offer
left outer join OffreMarket OM on (OM.codeOffer = Offer.code)

答案 2 :(得分:2)

如果您使用FMDB作为包装,那么答案就是:

@try {

    // Select Contact Details From Modules
    NSString *selectSQL = [NSString stringWithFormat:
                           @"SELECT * FROM %@ INNER JOIN %@ ON %@.%@=%@.%@ ;",
                           OffreMarket,
                           Offer,
                           OffreMarket
                           code
                           Offer
                           code];

    //NSLog*(@"Get All Offers select SQL: %@", selectSQL);
    FMResultSet *resultSet = [db executeQuery:selectSQL];
    NSMutableArray *marketOffers = [[NSMutableArray alloc]init];

    while ([resultSet next]) {

        // Create Offers Details Modal
        Offer *offer = [[Offer alloc] init];

         offer.code = [resultSet stringForKey:@"code"];
         offer.name = [resultSet stringForKey:@"name"];

        [marketOffers addObject: offer];
    }
    return (NSArray *)infos;
}
@catch (NSException *exception) {

    //NSLog*(@"%@ : %@",exception.name,exception.reason);
    return nil;
}
return nil;

尝试上面的代码。它会在最短的时间内获得所有数据。 FMDB是数据库操作的不错选择。