代码小记

2018-09-27  本文已影响6人  大胖_康

最近公司事情不是很多,然后就整理一下自己在工作中比较重要的代码.

单例的使用

/*
 专门用来保存单例代码
 最后一行不要加 \ 
*/

// @interface
#define singleton_interface(className) \
+ (className *)shared##className;


// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
+ (className *)shared##className \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
}

FMDB的操作(事务+线程安全)

***数据库路径***   
   #define dbPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0] stringByAppendingPathComponent:@"test.db"]

***创建数据库***
-(BOOL)createDBTable{
    //创建数据库文件
    BOOL result = NO;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断数据时是否存在
    BOOL isExist = [fileManager fileExistsAtPath:dbPath];
    if (!isExist) {
        NSMutableArray *sqlArr = [[NSMutableArray alloc]init];            
        // 创建表-订单的表-master头  obj5:退货才有,关联的原始凭证的sid
        [sqlArr addObject:@"CREATE TABLE IF NOT EXISTS  'master' ('sid' varchar NOT NULL PRIMARY KEY,'imel' varchar,'type' int)"];
        
        // 创建
        result = [self execute:sqlArr];
    }else{
        result = YES;
    }
    return result;
}

***对原有数据库进行增加字段,增加表***
-(BOOL)executeAddnew{
    __block BOOL result = NO;
    FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
    [dbQueue inDatabase:^(FMDatabase *db) {
        [db beginTransaction];
        //增加表
        if (![db tableExists:@"TicketInfo"]) {
            [db executeUpdate:@"CREATE TABLE  IF NOT EXISTS  'TicketInfo' ('ticketSid' varchar  NOT NULL PRIMARY KEY, 'storeNo' varchar, 'posNo' varchar, 'transNo' varchar, 'price' float, 'date' varchar, 'flag' int)"];
        //增加字段
        if ([db tableExists:@"Printer"]) {
            if (![db columnExists:@"Printer" inTableWithName:@"sid"]) {
                [db executeUpdate:@"ALTER TABLE 'Printer' ADD 'sid' varchar"];
            }
        }    
            
        }
        result = [db commit];

    }];
    [dbQueue close];
    return result;
}

***插入多条语句***
-(BOOL)execute:(NSMutableArray*)sqlStrArr
{
    __block BOOL result = NO;
    FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
    [dbQueue inDatabase:^(FMDatabase *db) {
        [db beginTransaction];
        for (NSString *sqlStr in sqlStrArr) {
            BOOL detailResult = [db executeUpdate:sqlStr];
            if (!detailResult) {
                [db rollback];
                break;
            }
        }
        result = [db commit];
    }];
    return result;
}

***插入单条语句***
-(BOOL)executeSingle:(NSString*)sqlStr
{
    __block BOOL result = NO;
    FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
    [dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:sqlStr];
        if (!result) {
            [db rollback];
        }
    }];
    return result;
}

***查询***
-(NSArray *)queryGoodsTypeWithSqlite:(NSString *)sqlite{
    __block NSMutableArray *marray = [[NSMutableArray alloc]initWithCapacity:10];
    FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
    [dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sqlite];
        while ([result next]) {
            GoodsTypeInfoModel *goodsTypeModel = [[GoodsTypeInfoModel alloc]init];
            goodsTypeModel.goodSid = [result stringForColumn:@"goodSid"];
            [marray addObject: goodsTypeModel];
        }
    }];
    return marray;
}

AFNetworking的封装

时间处理

JSON解析

见GitHub地址:https://github.com/Sunkangp/TOOL

上一篇下一篇

猜你喜欢

热点阅读