iOS FMDB的使用

作者: 追梦小怪兽 | 来源:发表于2018-04-11 18:21 被阅读0次

    FMDB在项目中的使用

    一. 首先导入FMDB三方库:在podfile 文件中添 pod 'FMDB' 然后在终端cd 到你的项目文件中,pod install 命令。FMDB的下载地址

    二. 在你需要的地方导入头文件:#import "FMDatabase.h"

    代码

    下面我会把FMDB的增、删、查、改放在一个工具类中,需要用到的地方直接用这个工具类的类方法去调用类方法。

    1. 首先我们肯定要去创建一个表。
    // 建表
    + (void)creatPictureDB{
        // document path
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
        // pic 表的文件路劲
        NSString *filePath = [path stringByAppendingString:@"/Picture.data"];
        NSLog(@"Picture.data filePath is %@",filePath);
        // 初始化数据库
        FMDatabase *db = [FMDatabase databaseWithPath:filePath];
        
        if ([db open]) // 想要对表进行操作,db 必须打开
        {
            // 创建表
            BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS PictureDB (id integer PRIMARY KEY AUTOINCREMENT, image_id integer NOT NULL,create_time text NOT NULL,camera integer NOT NULL,thumb_name text NOT NULL,file_name text NOT NULL,posData text NOT NULL,addressStr text NOT NULL,like text NOT NULL,downLoad text NOT NULL,province text NOT NULL,city text NOT NULL,district text NOT NULL);"]; // 这里是你这个表对应的model 的属性
            if (result){
                NSLog(@"创建表成功");
            }else{
                NSLog(@"创建表失败");
            }
        }
        
    }
    
    1. 插入数据
    // 插入数据
    + (void)insertPictureInPictureDB:(PictureDBModel*)pic{
            // 这里需要拿到db
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/Picture.data"];
        FMDatabase *db = [FMDatabase databaseWithPath:filePath];
        // 将地理位置数组转换成json 字符串
        NSString *locationArrString = [pic.posData mj_JSONString]; // 这里是一个数据,需要先将数组转换乘json 串
            if ([db open]){  // 如果有这个数据了。就不要添加进去了
                FMResultSet *rs = [db executeQuery:@"SELECT * FROM PictureDB WHERE file_name = ?",pic.file_name];
                while ([rs next]) {
                    NSLog(@"有这个数据了。不要加进来");
                    return;
                }
                BOOL result = [db executeUpdate:@"INSERT INTO PictureDB (image_id, create_time, camera, thumb_name, file_name, posData, addressStr, like, downLoad, province, city, district) VALUES(?,?,?,?,?,?,?,?,?,?,?,?);" withArgumentsInArray:@[@(pic.image_id),pic.create_time,@(pic.camera),pic.thumb_name,pic.file_name,locationArrString,pic.addressStr,pic.like,pic.downLoad,pic.province,pic.city,pic.district]];
                if (result){
    //                NSLog(@"插入数据成功");
                }else{
                    NSLog(@"插入数据失败");
                }
            }
        
    }
    
    1. 根据条件查找数据
    // 根据条件查找
    + (NSMutableArray *)checkDBWithStr:(NSString *)str{
        
        NSMutableArray * resultArr = [NSMutableArray new];
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/Picture.data"];
        FMDatabase *db = [FMDatabase databaseWithPath:path];
        if ([db open]){
    
            FMResultSet *rs = [db executeQuery:[NSString stringWithFormat:@"select * from PictureDB where addressStr like '%@%%' or addressStr like '%%%@%%'",str,str]];
            FMResultSet *rs1 = [db executeQuery:[NSString stringWithFormat:@"select * from PictureDB where create_time like'%@%%' or create_time like '%%%@%%'",str,str]];
            
            while ([rs next]) {
                PictureDBModel *model = [[PictureDBModel alloc] init];
                model.image_id = [[rs stringForColumn:@"image_id"] intValue];
                model.create_time = [rs stringForColumn:@"create_time"];
                model.camera = [[rs stringForColumn:@"camera"] intValue];
                model.thumb_name = [rs stringForColumn:@"thumb_name"];
                model.file_name = [rs stringForColumn:@"file_name"];
                model.posData = [rs stringForColumn:@"posData"];
                model.posDataArr = [PictureDBTool toArrayOrNSDictionary:[rs stringForColumn:@"posData"] ? [rs stringForColumn:@"posData"]:@""];
                model.addressStr = [rs stringForColumn:@"addressStr"];
                model.province = [rs stringForColumn:@"province"];
                model.city = [rs stringForColumn:@"city"];
                model.district = [rs stringForColumn:@"district"];
                model.like = [rs stringForColumn:@"like"];
                model.downLoad = [rs stringForColumn:@"downLoad"];
                [resultArr addObject:model];
            }
            
            while ([rs1 next]) {
                PictureDBModel *model = [[PictureDBModel alloc] init];
                model.image_id = [[rs1 stringForColumn:@"image_id"] intValue];
                model.create_time = [rs1 stringForColumn:@"create_time"];
                model.camera = [[rs1 stringForColumn:@"camera"] intValue];
                model.thumb_name = [rs1 stringForColumn:@"thumb_name"];
                model.file_name = [rs1 stringForColumn:@"file_name"];
                model.posData = [rs1 stringForColumn:@"posData"];
                model.posDataArr = [PictureDBTool toArrayOrNSDictionary:[rs1 stringForColumn:@"posData"]? [rs1 stringForColumn:@"posData"]:@""];
                model.addressStr = [rs1 stringForColumn:@"addressStr"];
                model.province = [rs1 stringForColumn:@"province"];
                model.city = [rs1 stringForColumn:@"city"];
                model.district = [rs1 stringForColumn:@"district"];
                model.like = [rs stringForColumn:@"like"];
                model.downLoad = [rs stringForColumn:@"downLoad"];
                
                [resultArr addObject:model];
            }
        
        }
        
        return resultArr;
    }
    
    // 我这里是有2个查找的条件,一个是地址,一个是时间。你可以根据自己的需求来筛选。
    

    4.根据自己的需求修改表中数据

    // 修改数据
    + (void)downLoadPictureWithFileName:(NSString *)filePath{
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"/Picture.data"];
        FMDatabase *db = [FMDatabase databaseWithPath:path];
        if ([db open]){
            BOOL result = [db executeUpdate:@"update PictureDB set downLoad = ? where file_name = ?",@"1",filePath];
            if (result){
                NSLog(@"修改数据成功");
                [AlertView alertViewWithTitle:@"添加本地相册成功"];
            }else{
                NSLog(@"修改数据失败");
                [AlertView alertViewWithTitle:@"添加本地相册失败"];
            }
        }
    }
    
    
    1. 删除表
    删除表的sql 语句是:" DROP TABLE IF EXISTS PictureDB"
    

    小结

    • 上面的代码是我项目中用到的,效仿时只需要稍微修改就行了,对FMDB的使用主要是对sql 语句的操作。

    ---来自涛胖子的工作笔记

    相关文章

      网友评论

        本文标题:iOS FMDB的使用

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