FMDB的增删改查

#import "FMDBManager.h"

@interface FMDBManager ()

@property (nonatomic, strong) FMDatabase *dataBase;

@end

@implementation FMDBManager

+ (FMDBManager *)shareInstance {
    
    static FMDBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        manager = [[FMDBManager alloc] init];
        
    });
    
    return manager;
}
//创建表
- (void)createTableWithTableName:(NSString *)tableName {
    
    
    [self initFMDB];
    
    
    if ([self.dataBase open]) {
        
        NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (resourceId INTEGER PRIMARY KEY AUTOINCREMENT, resourceName TEXT, resourceType TEXT, resourceLocalName TEXT, resourceSize TEXT, downProgress TEXT, downLoadStatus Text)",tableName];
        
        BOOL result = [self.dataBase executeUpdate:createSQL];
        
        if (result) {
            NSLog(@"创建成功");
        } else {
            NSLog(@"创建失败");
        }
    };
    [self.dataBase close];
}

//插入
- (void)insertResourceWithResourceType:(NSString *)resourceType
                            resourceId:(NSInteger )resourceId
                          resourceName:(NSString *)resourceName
                          resourceSize:(NSString *)resourceSize
                     resourceLocalName:(NSString *)resourceLocalName
                      downLoadProgress:(CGFloat   )downLoadProgress
                        downLoadStatus:(NSString *)downLoadStatus
                             tableName:(NSString *)tableName{
    
    [self initFMDB];
    
    if ([self.dataBase open]) {
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO '%@' (resourceId, resourceName, resourceType, resourceSize, resourceLocalName, downProgress, downLoadStatus) VALUES ('%ld', '%@', '%@', '%@', '%@', '%f', '%@')", tableName, (long)resourceId, resourceName, resourceType, resourceSize, resourceLocalName, downLoadProgress, downLoadStatus];
        
        BOOL result = [self.dataBase executeUpdate:insertSQL];
        
        if (result) {
            NSLog(@"插入成功");
        }
    }
    [self.dataBase close];
    
}


//查询所有
- (NSMutableArray *)selectAllResourceFromTableName:(NSString *)tableName {
    
    //返回的数组
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    
    [self initFMDB];
    if ([self.dataBase open]) {
        
        NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM %@", tableName];
        
        FMResultSet *result = [self.dataBase executeQuery:selectSQL];
        
        while ([result next]) {
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            
            NSInteger resourceId = [result longForColumn:@"resourceId"];
            NSString *resourceName = [result stringForColumn:@"resourceName"];
            NSString *resourceType = [result stringForColumn:@"resourceType"];
            NSString *resourceSize = [result stringForColumn:@"resourceSize"];
            NSString *resourceLocalName = [result stringForColumn:@"resourceLocalName"];
            NSString *downLoadProgress = [result stringForColumn:@"downProgress"];
            NSString *downLoadStatus = [result stringForColumn:@"downLoadStatus"];
            
            //NSLog(@"___result:___%ld \n %@ \n %@ \n %@",resourceId, resourceName, resourceType, resourceAddr);
            //整理成 json 返回
            [dic setValue:@(resourceId) forKey:@"resId"];
            [dic setValue:resourceName forKey:@"name"];
            [dic setValue:resourceType forKey:@"type"];
            [dic setValue:resourceSize forKey:@"size"];
            [dic setValue:resourceLocalName forKey:@"localName"];
            [dic setValue:downLoadProgress forKey:@"progress"];
            [dic setValue:downLoadStatus forKey:@"downLoadStatus"];
            
            
            [resultArr addObject:dic];
        }
        
        [result close];
    }
    
    [self.dataBase close];
    
    return resultArr;
    
}
//更新
- (void)upDateLocalNameTable:(NSString *)tableName
                   localName:(NSString *)localName
                resourceName:(NSString *)resourceName {
    
    [self initFMDB];
    
    if ([self.dataBase open]) {
        
        NSString *updateSQL = [NSString stringWithFormat:@"UPDATE %@ SET resourceLocalName = '%@' WHERE resourceName = '%@'", tableName, localName, resourceName];
        
        BOOL result = [self.dataBase executeUpdate:updateSQL];
        if (result) {
            NSLog(@"~~~~更新成功");
        } else {
            NSLog(@"~~~~~更新失败");
        }
        
    }
    
    [self.dataBase close];

    
}

//删除
- (void)deleteSelectedItemWithResourceName:(NSString *)resourceName
                                 fromTable:(NSString *)tableName{
    
    
    [self initFMDB];
    
    if ([self.dataBase open]) {
        
        NSString *deleteSql = [NSString stringWithFormat:
                               @"delete from %@ where resourceName = '%@'",
                               tableName, resourceName];
        BOOL res = [self.dataBase executeUpdate:deleteSql];
        
        if (res) {
            NSLog(@"删除成功");
            
        } else {
            NSLog(@"删除失败");
        }
    }
    [self.dataBase close];
}

//删除表
- (void)deleteAllWithTableName:(NSString *)tableName {
    
    
    
    [self initFMDB];
    
    if ([self.dataBase open]) {
        
        NSString *clearSQL = [NSString stringWithFormat:@"DROP TABLE IF EXISTS %@", tableName];
        BOOL result = [self.dataBase executeUpdate:clearSQL];
        
        if (result) {
            NSLog(@"清除表数据成功");
        } else {
            NSLog(@"清除表数据失败");
        } 
    }
}

//初始化
- (void)initFMDB {
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *fileName = [doc stringByAppendingPathComponent:@"resource.sqlite"];
    
    NSLog(@"%@", fileName);
    
    self.dataBase = [FMDatabase databaseWithPath:fileName];
    
}



FMDB的封装,使你的实体类具备数据库操作的功能,极大简化你的数据库操作,对于自己的扩展也非常简单。 该框架是本人在项目中用到的对FMDB的封装,它的特点如下: 1.自动创建数据库、自动创建数据库表。 2.自动检测字段添加新字段。 3.一行代码实现数据库的CURD操作。 4.源码及其简单,易于理解和掌握。 5.扩展自己的功能也非常得简单,容易。 6,只关心字段和字段值,完全不用关心数据库操作逻辑. 特别提示:字段值全部以字符串的形式统一处理 常用的api如下: /** 获取数据库单例函数 */ (instancetype)intance; /** 数据库中是否存在表 */ - (BOOL)isExistWithTableName:(NSString*)name; /** 默认建立主键id 创建表(如果存在则不创建) , keys 数据存放要求@[字段名称1,字段名称2] */ -(BOOL)createTableWithTableName:(NSString*)name keys:(NSArray*)keys; /** 插入 只关心key和value @{key:value,key:value} */ -(BOOL)insertIntoTableName:(NSString*)name Dict:(NSDictionary*)dict; /** 根据条件查询字段 返回的数组是字典( @[@{key:value},@{key:value}] ) ,where形式 @[@"key",@"=",@"value",@"key",@">=",@"value"] */ -(NSArray*)queryWithTableName:(NSString*)name keys:(NSArray*)keys where:(NSArray*)where; /** 全部查询 返回的数组是字典( @[@{key:value},@{key:value}] ) */ -(NSArray*)queryWithTableName:(NSString*)name; /** 根据key更新value 形式 @[@"key",@"=",@"value",@"key",@">=",@"value"] */ -(BOOL)updateWithTableName:(NSString*)name valueDict:(NSDictionary*)valueDict where:(NSArray*)where; /** 根据表名和表字段删除表内容 where形式 @[@"key",@"=",@"value",@"key",@">=",@"value"] */ -(BOOL)deleteWithTableName:(NSString*)name where:(NSArray*)where; /** 根据表名删除表格全部内容 */ -(BOOL)clearTable:(NSString*)name; /** 删除表 */ -(BOOL)dropTable:(NSString*)name;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值