大师网-带你快速走向大师之路 解决你在学习过程中的疑惑,带你快速进入大师之门。节省时间,提升效率

FMDB的应用--猫,你看看

好基友问有没有简单方便的本地化第三方,果断推荐FMDB。
然并卵,好基友不会用,为了我们的小船,还是写个使用方法吧。
首先我们需要一个专门操作这个数据库的类,

#import <Foundation/Foundation.h>
@class Model;

@interface DBManager : NSObject
//创建数据库
- (BOOL)createDataBaseWithName:(NSString *)name;
//创建表
- (BOOL)createTableWithDic:(NSArray *)arr;
//插入一个model
- (BOOL)insertWithModel:(Model *)model;
//删除表
- (BOOL)deleteDataBase;
//删除一条记录
- (BOOL)deleteDataBaseWithModel:(Model *)model;
//查询所有的
- (NSArray *)selectAllModels;
//查询一条记录
- (Model *)selectDataWithId:(int)id;
@end

其中Model是你需要操作的model.
具体实现如下:

#import "DBManager.h"
#import "FMDB.h"
#import "Model.h"

@interface DBManager (){
    FMDatabase *dateBase;
}

@end

@implementation DBManager
//创建数据库
- (BOOL)createDataBaseWithName:(NSString *)name{
    //创建数据库
    NSString *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    dateBase = [FMDatabase databaseWithPath:[path stringByAppendingString:[NSString stringWithFormat:@"/%@",name]]];
    return [dateBase open];
}
//创建表
- (BOOL)createTableWithDic:(NSArray *)arr{
    NSMutableString *creat = [NSMutableString stringWithString:@"create table if not exists t_user(id integer primary key)"];
    for (NSString *per in arr) {
        [creat insertString:[NSString stringWithFormat:@",%@ varchar", per] atIndex:creat.length-1];
    }
    return [dateBase executeUpdate:creat];
}

//插入一个model
- (BOOL)insertWithModel:(Model *)model{

    BOOL isBe = [self selectDataWithId:[model.id intValue]];
    if (isBe) {
        return [self updateDataWithModel:model];
    } else {
        NSString * insertSql = [NSString stringWithFormat:@"insert into t_user(id,name,sex) values('%@','%@','%@')", model.id, model.name, model.sex];
        return [dateBase executeUpdate:insertSql];
    }
}
//更新数据
- (BOOL)updateDataWithModel:(Model *)model{
    NSString *update = [NSString stringWithFormat:@"update t_user set name = '%@', sex = '%@' where id = '%@'", model.name, model.sex, model.id];
    return [dateBase executeUpdate:update];
}
//删除表
- (BOOL)deleteDataBase{
    NSString * delete=@"delete from t_user";
    return [dateBase executeUpdate:delete];
}
//删除一条记录
- (BOOL)deleteDataBaseWithModel:(Model *)model{
    NSString * delete = [NSString stringWithFormat:@"delete from t_user where id = '%@'",model.id];
    return [dateBase executeUpdate:delete];
}
//查询所有的
- (NSArray *)selectAllModels{
    NSMutableArray *results = [NSMutableArray array];
    NSString * sql = @"select * from t_user";
    FMResultSet *result = [dateBase executeQuery:sql];
    while(result.next){
        Model *model = [self selectDataWithResult:result];
        [results addObject:model];
    }
    return results;
}
//查询一条记录
- (Model *)selectDataWithId:(int)id{
    NSString * sql = [NSString stringWithFormat:@"select * from t_user where id = '%d'",id];
    FMResultSet *result = [dateBase executeQuery:sql];
    Model *model;
    if ([result next]) {
        model = [self selectDataWithResult:result];
    }
    return model;
}

- (Model *)selectDataWithResult:(FMResultSet *)result{
    Model *model = [[Model alloc] init];
    int ids = [result intForColumn:@"id"];
    NSString *name = [result stringForColumn:@"name"];
    NSString *sex = [result stringForColumn:@"sex"];
    model.id = [NSNumber numberWithInt:ids];
    model.name = name;
    model.sex = sex;
    return model;
}

@end

这样一个简单的FMDN使用类就写完了。
具体使用如下:

#import "ViewController.h"
#import "DBManager.h"
#import "Model.h"

@interface ViewController (){
    DBManager *manager;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    manager = [[DBManager alloc] init];
    //创建数据库
    BOOL open = [manager createDataBaseWithName:@"model.splist"];
    //打开数据库
    if (open) {
        NSLog(@"数据库打开成功");
    }

    //创建表
    BOOL createTable = [manager createTableWithDic:@[@"name", @"sex"]];
    if (createTable) {
        NSLog(@"创建表成功");
    }

    for (int i = 0; i < 10; i++) {
        Model *model = [[Model alloc] init];
        model.id = [NSNumber numberWithInt:i];
        model.name = [NSString stringWithFormat:@"name%d",i];
        BOOL success = [manager insertWithModel:model];
        if (success) {
            NSLog(@"添加成功");
        }
    }

    Model *model = [manager selectDataWithId:3];
    NSLog(@"%@", model);
    NSArray *arr = [manager selectAllModels];
    NSLog(@"%@", arr);

    [manager deleteDataBaseWithModel:model];
    NSLog(@"%@", [manager selectDataWithId:3]);


    // Do any additional setup after loading the view, typically from a nib.
}

不过如果遇到多线程的话就会有问题,所以如果你需要的话用FMDatabaseQueue,不过我没用,因为只是为了给好基友一个简单的提示还是偷点懒,当然在管理类里面还可以把model转化成字典,然后来进行各种操作。