美文网首页
iOS FMDB简单封装(临时购物车为例)

iOS FMDB简单封装(临时购物车为例)

作者: 雨洒潇湘 | 来源:发表于2018-01-11 10:47 被阅读39次

    项目需求

    最近项目要添加一个临时购物车, 就是在没有登录的情况下,用户可以对购物车进行修改删除添加单选多选等操作。而且用户清除缓存数据还会存在。这样的话就得需要创建一张表,将商品的信息添加进去。从而对表进行增删改查操作。

    代码实现

    .h

    
    #import <Foundation/Foundation.h>
    
    @interface FMDBData : NSObject
    
    
    //插入数据
    + (void)insertDatawithGoods:(NSDictionary *)goods;
    //修改全部数据
    + (void)updateWithGoodsList:(NSDictionary *)goods;
    //修改单条数据
    + (void)udateWithGoods:(NSDictionary *)goods;
    //删除数据
    + (void)removeGoods:(NSString *)goodsId;
    //查询数据
    + (NSArray *)selectGoodsList;
    //删除数据库
    + (BOOL)dropGoodsList;
    @end
    
    

    .m

    #import "FMDBData.h"
    #import <FMDB/FMDB.h>
    
    @implementation FMDBData
    static FMDatabase *_db;
    
    +(void)initialize {
        [FMDBData initDBwithName:@"goodsList"];
    }
    //创建数据库
    + (void)initDBwithName:(NSString *)name {
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *fileName = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.selite", name]];
        FMDatabase *db = [FMDatabase databaseWithPath:fileName];
        if ([db open]) {
            BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS goodsList(name text NOT NULL, imageUrl text NOT NULL, price text NOT NULL, goodsID text NOT NULL, stock integer NOT NULL, number integer NOT NULL, weight integer NOT NULL, isSelect integer NOT NULL);"];
    //这张表中一条数据有八个属性,包括基本的图片地址(imageUrl),价格信息(price),数量(number),是否选中(isSelect),货品ID(goodsID),重量(weight),库存(stock),商品名字(name);
            if (result) {
                NSLog(@"创表成功");
            } else {
                NSLog(@"创表失败");
            }
        }
        _db = db;
    }
    
    + (void)insertDatawithGoods:(NSDictionary *)goods {
    
        BOOL isGoods = YES;
        NSString *goodsID = [goods valueForKey:@"goodsID"];
        NSInteger oldNumber = [[goods valueForKey:@"number"]integerValue];
        //查询
        FMResultSet *resultSet = [_db executeQuery:@"SELECT * FROM goodsList WHERE goodsID = ?",goodsID];
        while ([resultSet next]) {
            isGoods = NO;
            //修改在加入购物者界面不知道购物车中是否有相同商品,如果没有则插入数据,如果有则修改数据
            NSInteger number = [resultSet intForColumn:@"number"] + oldNumber;
         BOOL result = [_db executeUpdate:@"UPDATE goodsList SET number = ? WHERE goodsID = ?", @(number), goodsID];
            if (result) {
                NSLog(@"修改成功");
            }  else {
                NSLog(@"修改失败");
            }
        }
        if (isGoods) {
            //插入
           BOOL result = [_db executeUpdate:@"INSERT INTO goodsList (name, imageUrl, price, goodsID, stock, number, weight, isSelect) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", [goods valueForKey:@"name"], [goods valueForKey:@"imageUrl"], [goods valueForKey:@"price"], goodsID, @([[goods valueForKey:@"stock"] integerValue]), @(oldNumber), @([[goods valueForKey:@"weight"] integerValue]), @([[goods valueForKey:@"isSelect"] integerValue])];
            if (result) {
                NSLog(@"插入成功");
            } else {
                NSLog(@"插入失败");
            }
        }
    }
    
    + (void)udateWithGoods:(NSDictionary *)goods {
        BOOL isSelect = NO;
        NSString *goodsID = [goods valueForKey:@"goodsID"];
        NSArray *keyArr = goods.allKeys;
        for (NSString *key in keyArr) {
            if ([key isEqualToString:@"isSelect"]) {
                isSelect = YES;
            }
        }
        if (isSelect) {
            NSInteger oldNumber = [[goods valueForKey:@"isSelect"] integerValue];
            if (goodsID == nil) {
                BOOL result = [_db executeUpdate:@"UPDATE goodsList SET isSelect = ?", @(oldNumber)];
                if (result) {
                    NSLog(@"修改成功");
                }  else {
                    NSLog(@"修改失败");
                }
            } else {
                //查询
                FMResultSet *resultSet = [_db executeQuery:@"SELECT * FROM goodsList WHERE goodsID = ?",goodsID];
                while ([resultSet next]) {
                    //修改
                    BOOL result = [_db executeUpdate:@"UPDATE goodsList SET isSelect = ? WHERE goodsID = ?", @(oldNumber), goodsID];
                    if (result) {
                        NSLog(@"修改成功");
                    }  else {
                        NSLog(@"修改失败");
                    }
                }
            }
    
        } else {
            NSInteger oldNumber = [[goods valueForKey:@"number"] integerValue];
            //查询
            FMResultSet *resultSet = [_db executeQuery:@"SELECT * FROM goodsList WHERE goodsID = ?",goodsID];
            while ([resultSet next]) {
                //修改
                BOOL result = [_db executeUpdate:@"UPDATE goodsList SET number = ? WHERE goodsID = ?", @(oldNumber), goodsID];
                if (result) {
                    NSLog(@"修改成功");
                }  else {
                    NSLog(@"修改失败");
                }
            }
        }
    
    }
    
    + (void)updateWithGoodsList:(NSDictionary *)goods {
        NSInteger oldNumber = [[goods valueForKey:@"isSelect"] integerValue];
        //查询
        FMResultSet *resultSet = [_db executeQuery:@"SELECT * FROM goodsList?"];
        while ([resultSet next]) {
            //修改
            BOOL result = [_db executeUpdate:@"UPDATE goodsList SET isSelect = ?", @(oldNumber)];
            if (result) {
                NSLog(@"修改成功");
            }  else {
                NSLog(@"修改失败");
            }
        }
    }
    
    + (void)removeGoods:(NSString *)goodsId {
        BOOL result;
        if ([goodsId isEqualToString:@"1"]) {
             result = [_db executeUpdate:@"DELETE FROM goodsList WHERE isSelect = 1"];
        } else {
             result = [_db executeUpdate:@"DELETE FROM goodsList WHERE goodsID = ?", goodsId];
    
        }
        if (result) {
            NSLog(@"删除成功");
        } else {
            NSLog(@"删除失败");
        }
    }
    
    + (NSArray *)selectGoodsList {
        FMResultSet *resultSet = [_db executeQuery:@"SELECT * FROM goodsList"];
        NSMutableArray * goodsarr = [NSMutableArray array];
        while ([resultSet next]) {
            NSString *name = [resultSet stringForColumn:@"name"];
            NSString *imageUrl = [resultSet stringForColumn:@"imageUrl"];
            NSString *price = [resultSet stringForColumn:@"price"];
            NSString *goodsID  = [resultSet stringForColumn:@"goodsID"];
            NSInteger stock = [resultSet intForColumn:@"stock"];
            NSInteger number = [resultSet intForColumn:@"number"];
            NSInteger isSelect = [resultSet intForColumn:@"isSelect"];
            NSInteger weight = [resultSet intForColumn:@"weight"];
            NSDictionary *dic = @{@"name": name, @"imageUrl": imageUrl, @"price": price, @"goodsID": goodsID, @"stock": @(stock), @"number": @(number), @"isSelect":@(isSelect), @"weight":@(weight)};
            [goodsarr addObject:dic];
        }
        return goodsarr;
    }
    
    + (BOOL)dropGoodsList {
        return [_db executeUpdate:@"DELETE FROM goodsList"];
    }
    
    @end
    

    总的来说FMDB还是挺好用的,关键在于你去如何封装。

    相关文章

      网友评论

          本文标题:iOS FMDB简单封装(临时购物车为例)

          本文链接:https://www.haomeiwen.com/subject/lohznxtx.html