小记iOS使用FMDB存储自定义数据模型到数据库

我这里贴出自己项目中的代码,TMYoGoodsdb是自定义模型,为了方便观看,我这里省略了部分变量,就是代码里面...处

#import "TMYoDataManager.h"
#import 
#import "TMYoGoodsdb.h"

@interface TMYoDataManager()

@property (nonatomic, strong) FMDatabase *db;

@end

@implementation TMYoDataManager

//单例
+ (instancetype)shareManager {
    static TMYoDataManager* manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager =[[self alloc] init];
    });
    return manager;
}

// 新建数据库并打开
- (BOOL)creatYoGoodsDB {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"YoGoods0.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:path];
    self.db = db;
    if ([db open]) {
        NSString *sqlStr = @"CREATE TABLE IF NOT EXISTS YoGoods0 (remark text,shopNo text,...)";
        if (![self.db executeUpdate:sqlStr]) {
            NSLog(@"创建表失败!");
            [self.db close];
            return NO;
        }
    }else{
        [self.db close];
        return NO;
    }
    return YES;
}

//写入数据
- (void)insertGoodsData:(TMYoGoodsdb *)goods{
    NSString *sql = @"insert into YoGoods0 (remark,shopNo, ...) values (?,?,...)";
    [self.db executeUpdate:sql,goods.shopNo,goods.goodsName,...];
}

//修改数据
- (void)changeGoodsData:(TMYoGoodsdb *)goods{
    NSString *specSql = [NSString stringWithFormat:@"select *from YoGoods0 where remark = '%@'",goods.goodsNo];
    FMResultSet *specRs = [self.db executeQuery:specSql];
    if ([specRs next]) {
        NSString *sql = @"update YoGoods0 set num = ? where remark = ?";
        BOOL res = [self.db executeUpdate:sql, [NSNumber numberWithInteger:goods.num]];
        if (!res) {
            NSLog(@"修改失败");
        }
    }else{
        [self insertGoodsData:goods];
    }
}

//按条件删除数据
- (void)deleteGoodsData:(TMYoGoodsdb *)goods{
    NSString *sql = @"delete from YoGoods0 where remark = ?";
    [self.db executeUpdate:sql,goods.goodsNo];
}

//查询表所有数据
- (NSMutableArray *)queryAllGoodsData{
    NSMutableArray *mutArray = [NSMutableArray array];
    NSString *sql = @"select *from YoGoods0";
    FMResultSet *rs = [self.db executeQuery:sql];
    while ([rs next]) {
        NSString *shopNo = [rs stringForColumn:@"shopNo"];
        NSString *goodsName = [rs stringForColumn:@"goodsName"];
        ...
        TMYoGoodsdb *goods = [TMYoGoodsdb initWith:shopNo
                                         goodsName:goodsName
                                           ...];
        [mutArray addObject:goods];
    }
    return mutArray;
}

//按条件查询,比如我这里根据店铺编号查询
- (NSMutableArray *)queryShopGoods:(NSString *)shopNo{
    NSMutableArray *mutArray = [NSMutableArray array];
    NSString *sql = [NSString stringWithFormat:@"select *from YoGoods0 where shopNo = '%@'",shopNo];
    FMResultSet *rs = [self.db executeQuery:sql];
    while ([rs next]) {
        NSString *shopNo = [rs stringForColumn:@"shopNo"];
        NSString *goodsName = [rs stringForColumn:@"goodsName"];
        ...
        TMYoGoodsdb *goods = [TMYoGoodsdb initWith:shopNo
                                         goodsName:goodsName
                                           ...];
        [mutArray addObject:goods];
    }
    return mutArray;
}

//按条件批量删除,比如我这里是根据店铺编号
- (void)clearYoGoodsDB:(NSString *)shopNo{
    NSMutableArray *allList = [self queryAllGoodsData];
    [self.db beginTransaction];//开启一个事务
    BOOL isRollBack = NO;
    @try {
        for (int i = 0;i < allList.count;i++) {
            NSString *sql = @"delete from YoGoods0 where shopNo = ?";
            BOOL res =  [self.db executeUpdate:sql,shopNo];
            if (!res) {
                [SVProgressHUD showInfoWithStatus:@"清空失败"];
            }
        }
    } @catch (NSException *exception) {
        isRollBack = YES;
        [self.db rollback];//回滚事务
    } @finally {
        if (!isRollBack) {
            [self.db commit];//重新提交事务
        }
    }
}


@end

 

你可能感兴趣的:(IOS)