你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS FMDB 查詢 批量更新

iOS FMDB 查詢 批量更新

編輯:IOS開發綜合
+ (NSMutableArray *)querygoodsByIdFromLocal:(NSString *)dayFlightId{
    FMDatabase *fmdb = [SADBManager sharedDB];
    if (!fmdb) {
        //失敗處理
        return nil;
    }
    //編寫SQL查詢語句
    NSString *query = [NSString stringWithFormat:@select * from FlightGoods where dayFlightId= '%@', dayFlightId];
    
    FMResultSet *rs = [fmdb executeQuery:query];
    
    NSMutableArray *invArray = [[NSMutableArray alloc]init];
    FlightGoods *flightgoods;
    
    while ([rs next]) {
        flightgoods = [[FlightGoods alloc]init];

        flightgoods.dayFlightId = [rs stringForColumn:@dayFlightId];
        flightgoods.goodsCode = [rs stringForColumn:@goodsCode];
        flightgoods.goodsName = [rs stringForColumn:@goodsName];
        flightgoods.goodsUnit = [rs stringForColumn:@goodsUnit];
        flightgoods.remarks = [rs stringForColumn:@remarks];
        flightgoods.cabinStockNum = [[NSNumber alloc]initWithInt:[rs intForColumn:@cabinStockNum]];
        flightgoods.loseNum = [[NSNumber alloc]initWithInt:[rs intForColumn:@loseNum]];
        flightgoods.actualNum = [[NSNumber alloc]initWithInt:[rs intForColumn:@actualNum]];
        flightgoods.tempActualNum = flightgoods.actualNum;// 這裡為tempActualNum賦值,之後根據這個值判斷能否加1操作
        flightgoods.tempLoseNum = flightgoods.loseNum;
        flightgoods.tempRemarks = flightgoods.remarks;
        
        [invArray addObject:flightgoods];
    }
    return invArray;
}

// 更新對象值到數據庫
+ (BOOL)updateGoodsByFlightGoods:(NSMutableArray *)flightGoodsArr{
    FMDatabase *fmdb = [SADBManager sharedDB];
    if (!fmdb) {
        //失敗處理
        return nil;
    }
    
    [fmdb beginTransaction];
    BOOL isRollBack = NO;
    @try {
        for (int i = 0; i<[flightGoodsArr count]; i++) {
            
            FlightGoods * flightGoods = [flightGoodsArr objectAtIndex:i];
            NSString * update = [NSString stringWithFormat:@update FlightGoods set actualNum = '%@', loseNum = '%@',remarks = '%@' where goodsCode = '%@',flightGoods.actualNum,flightGoods.loseNum,flightGoods.remarks,flightGoods.goodsCode];
            BOOL isSuccess = [fmdb executeUpdate:update];
    
            if (!isSuccess) {
                NSLog(@update Failure);
            }
        }
    }
    @catch (NSException *exception) {
        isRollBack = YES;
        [fmdb rollback];
    }
    @finally {
        if (!isRollBack) {
            [fmdb commit];
        }
    }
    
    return !isRollBack;

}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved