iOS 数据持久化之FMDB

新建一个项目名为QZFMDB,打开终端

cd /Users/hongde/Desktop/QZFMDB
vim Podfile

键盘敲’i’输入

target 'QZFMDB' do
pod 'JQFMDB'
end

“:wq”回车键

pod install

关掉项目 重新打开QZFMDB.xcworkspace

ViewController.m中

//
//  ViewController.m
//  QZFMDB
//

#import "ViewController.h"
#import "JQFMDB.h"
#import "Person.h"

@interface ViewController ()
@property (nonatomic, strong) JQFMDB *db;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubView];
    Person *person = [[Person alloc] init];
    person.name = @"cleanMonkey";
    person.phoneNum = @(188666666666);
    person.photoData = UIImagePNGRepresentation([UIImage imageNamed:@"bg.jpg"]);
    person.luckyNum = 7;
    person.sex = 0;
    person.age = 26;
    person.height = 172.12;
    person.weight = 120.4555;

    //用来测试操作一组数据
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 3; i++) {
        Person *person = [[Person alloc] init];
        person.name = [self randomName];
        person.phoneNum = @(18866668888);
        person.photoData = UIImagePNGRepresentation([UIImage imageNamed:@"bg.jpg"]);
        person.luckyNum = 7;
        person.sex = arc4random()%2;
        person.age = 26;
        person.height = 172.12;
        person.weight = 120.4555;

        [mArr addObject:person];
    }
    //这儿初始化的时候有三种 可以任选 有默认的
    JQFMDB *db = [JQFMDB shareDatabase:@"friend.sqlite"];
    self.db = db;
    NSLog(@"沙盒路径:%@", NSHomeDirectory());
    //上面是创建库但没有表 所以下面需要你创建表 注意有表后才能插入数据
    if (![db jq_isExistTable:@"friend"]) {
        [db jq_createTable:@"friend" dicOrModel:[Person class]];
    }
    //插入数据
    [db jq_insertTable:@"friend" dicOrModel:person];
    //也可以用这个 批量插入
    //[db jq_insertTable:@"friend" dicOrModelArray:mArr];

}

- (void)setupSubView
{
    UIButton *btn = [UIButton new];
    btn.frame = CGRectMake(20, 100, 100, 50);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100;
    [self.view addSubview:btn];

    UIButton *btn2 = [UIButton new];
    btn2.frame = CGRectMake(20, 160, 100, 50);
    btn2.backgroundColor = [UIColor redColor];
    [btn2 setTitle:@"更改" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 101;
    [self.view addSubview:btn2];

    UIButton *btn3 = [UIButton new];
    btn3.frame = CGRectMake(20, 220, 100, 50);
    btn3.backgroundColor = [UIColor redColor];
    [btn3 setTitle:@"查找" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn3.tag = 102;
    [self.view addSubview:btn3];

}
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%s",__func__);
    switch (btn.tag) {
        case 100:
            [self.db jq_deleteTable:@"friend" whereFormat:@"WHERE name = 'cleanMonkey'"];
            break;
        case 101: {
            [self.db jq_updateTable:@"friend" dicOrModel:@{@"name":@"testname"} whereFormat:@"where name = 'hbapega'"];
        }
            break;
        case 102:{
            NSArray *arr = [self.db jq_lookupTable:@"friend" dicOrModel:[Person class] whereFormat:@"where sex = '0'"];
            NSLog(@"%@",arr);
        }
        default:
            break;
    }

}
//获得随机字符名称
- (NSString *)randomName
{
    NSString *string = [[NSString alloc] init];
    for (int i = 0; i < 7; i++) {
        int figure = (arc4random() %26) + 97;
        char character = figure;
        NSString *tempString = [NSString stringWithFormat:@"%c",character];
        string = [string stringByAppendingString:tempString];
    }
    return string;
}

@end

Person.h中

//
//  Person.h
//  QZFMDB


#import 

@interface Person : NSObject
//可忽略,默认的主键ID,如果需要获取主键ID的值,可在自己的model中添加下面这个属性
@property (nonatomic, assign) NSInteger pkid;

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *phoneNum;
@property (nonatomic, strong) NSData *photoData;
@property (nonatomic, assign) NSInteger luckyNum;
@property (nonatomic, assign) BOOL sex;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) float height;//float 类型存入172.12会变成172.19995,取值时%.2f等于原值172.12
@property (nonatomic, assign) double weight;

//为了测试除以上类型外,下面的类型不参与建表
@property (nonatomic, strong) NSDictionary *testDic;
@property (nonatomic, strong) NSArray *testArr;
@property (nonatomic, strong) NSError *testError;
@property (nonatomic, strong) Person *testP;

@end

其实就是JQFMDB的简单应用,JQFMDB又是对FMDB的简单封装,我对原demo中的一些代码进行删除修改,把原来添加的view去掉,更直接更简单。

你可能感兴趣的:(iOS)