ios中使用FMDB數據的增刪改查

image

#使用cocoapods集成
####1. 初始化pod文件

pod init

####2. 編寫Podfile文件

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'MyFMDBDemo' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for MyFMDBDemo
	pod 'Masonry'          // 添加布局框架
	pod 'SDWebImage', '~> 5.0'   // 添加圖片加載框架
    pod 'FMDB'   // 添加封裝數據庫

  target 'MyFMDBDemoTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'MyFMDBDemoUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

####3. 安裝框架(記得重新打開項目時,選擇.xcworkspace那個文件)

pod install

####4. 在項目中使用框架

//
//  CZFFMDBExecute.m
//  MyFMDBDemo
//
//  Created by 陳帆 on 2019/11/7.
//  Copyright © 2019 陳帆. All rights reserved.
//

#import "CZFFMDBExecute.h"
#import "User.h"

// 集成FMDB框架
#import <fmdb/FMDB.h>

#define tableName @"User"

@interface CZFFMDBExecute()

/// 文件目錄
@property(nonatomic, copy) NSString *documentDir;
@property(nonatomic, copy) FMDatabase *database;

@end

@implementation CZFFMDBExecute

/// 單例
+ (instancetype)shareInstance {
    static CZFFMDBExecute *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance =[[CZFFMDBExecute alloc] init];
    });
    
    return instance;
}

#pragma mark - Data base operate
- (NSString *)documentDir {
    if (!_documentDir) {
        _documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    }
    
    return _documentDir;
}

- (FMDatabase *)database {
    if (!_database) {
        NSString *path = [self.documentDir stringByAppendingPathComponent:@"fmdb.db"];
        _database = [FMDatabase databaseWithPath:path];
        
        // create table
        if (![_database open]) {
            NSLog(@"database does not open.");
            return _database;
        }
        
        // 數據庫中創建表(可創建多張)
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ ('userId' TEXT,'username' TEXT NOT NULL, 'userIcon' TEXT ,'age' INTEGER, 'createTime' INTEGER, 'income' REAL, 'isMan' INTEGER)", tableName];
        BOOL result = [_database executeUpdate:sql];
        if (result) {
            NSLog(@"create table success");
        }
        
        [_database close];
    }
    return _database;
}


/// 增加用戶實體
/// @param user 用戶對象
- (void)addUser:(User *)user {
    if (![self.database open]) {
        NSLog(@"database does not open.");
        return;
    }
    NSString *execSql = [NSString stringWithFormat:@"INSERT INTO %@ VALUES('%@','%@','%@',%d,%ld,%f,%d)", tableName, user.userId, user.username, user.userIcon, user.age, (NSInteger)[user.createTime timeIntervalSince1970], [user.income floatValue], user.isMan ? 1 : 0];
    BOOL result = [self.database executeUpdate:execSql];
    if (result) {
        NSLog(@"add user success");
    }
    
    [self.database close];
}


/// 獲取用戶列表
- (NSArray<User *> *)getUserList {
    if (![self.database open]) {
        NSLog(@"database does not open.");
        return NULL;
    }
    
    NSString *execSql = [NSString stringWithFormat:@"SELECT * FROM %@", tableName];
    FMResultSet *result = [self.database executeQuery:execSql];
    NSMutableArray *arr = [NSMutableArray array];
    while ([result next]) {
        User *user = [User new];
        user.userId = [result stringForColumn:@"userId"];
        user.username = [result stringForColumn:@"username"];
        user.userIcon = [result stringForColumn:@"userIcon"];
        user.age = [result intForColumn:@"age"];
        user.createTime = [NSDate dateWithTimeIntervalSince1970:[result longForColumn:@"createTime"]];
        user.income = [[NSDecimalNumber alloc] initWithFloat:[result doubleForColumn:@"income"]];
        user.isMan = [result intForColumn:@"isMan"] == 0 ? NO : YES;
        [arr addObject:user];
    }
    
    [self.database close];
    
    return arr;
}

/// 刪除指定索引用戶
/// @param userId 用戶id
- (void)deleteUser:(NSString *)userId {
    if (![self.database open]) {
        NSLog(@"database does not open.");
        return;
    }
    NSString *execSql = [NSString stringWithFormat:@"delete from %@ where userId = '%@'", tableName, userId];
    BOOL result = [self.database executeUpdate:execSql];
    if (result) {
        NSLog(@"delete user success");
    }
    
    [self.database close];
}

/// 刪除所有用戶
- (void)deleteAllUser {
    if (![self.database open]) {
        NSLog(@"database does not open.");
        return;
    }
    NSString *execSql = [NSString stringWithFormat:@"delete from %@", tableName];
    BOOL result = [self.database executeUpdate:execSql];
    if (result) {
        NSLog(@"delete all user success");
    }
    
    [self.database close];
}

/// 更新(修改)用戶
/// @param user 用戶對象
- (void)updateUser:(User *)user {
    if (![self.database open]) {
        NSLog(@"database does not open.");
        return;
    }
    NSString *execSql = [NSString stringWithFormat:@"UPDATE %@ SET username = '%@', age = %d, isMan = %d WHERE userId = '%@'", tableName, user.username, user.age, user.isMan ? 1 : 0, user.userId];
    BOOL result = [self.database executeUpdate:execSql];
    if (result) {
        NSLog(@"update user success");
    }
    
    [self.database close];
}

@end

####5. 使用數據的增刪改查

//
//  ViewController.m
//  MyFMDBDemo
//
//  Created by 陳帆 on 2019/11/7.
//  Copyright © 2019 陳帆. All rights reserved.
//

#import "ViewController.h"
#import "CZFFMDBExecute.h"
#import "User.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) NSMutableArray *dataSource;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, weak) UIButton *addBtn;
@property(nonatomic, weak) UIButton *deleteBtn;
@property(nonatomic, weak) UIButton *modifyBtn;
@property(nonatomic, weak) UIButton *queryBtn;

@property(nonatomic, weak) UILabel *noDataTipLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setViewUI];
}

- (void)setViewUI {
    // 設置增加按鈕
    UIButton *addBtn = [[UIButton alloc] init];
    self.addBtn = addBtn;
    [addBtn setTitle:@"增加" forState:UIControlStateNormal];
    addBtn.backgroundColor = [UIColor blueColor];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 設置刪除按鈕
    UIButton *deleteBtn = [[UIButton alloc] init];
    self.deleteBtn = deleteBtn;
    [deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];
    deleteBtn.backgroundColor = [UIColor blueColor];
    [deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 設置修改按鈕
    UIButton *modifyBtn = [[UIButton alloc] init];
    self.modifyBtn = modifyBtn;
    [modifyBtn setTitle:@"修改" forState:UIControlStateNormal];
    modifyBtn.backgroundColor = [UIColor blueColor];
    [modifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    modifyBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [modifyBtn addTarget:self action:@selector(modifyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 設置查詢按鈕
    UIButton *queryBtn = [[UIButton alloc] init];
    self.queryBtn = queryBtn;
    [queryBtn setTitle:@"查詢" forState:UIControlStateNormal];
    queryBtn.backgroundColor = [UIColor blueColor];
    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [queryBtn addTarget:self action:@selector(queryBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 沒有數據提醒
    UILabel *noDataTipLabel = [[UILabel alloc] init];
    self.noDataTipLabel = noDataTipLabel;
    noDataTipLabel.text = @"沒有數據啦";
    noDataTipLabel.textColor = [UIColor lightGrayColor];
    noDataTipLabel.textAlignment = NSTextAlignmentCenter;
    noDataTipLabel.font = [UIFont systemFontOfSize:14.0];
    noDataTipLabel.numberOfLines = 0;
//    noDataTipLabel.hidden = YES;
    
    
    self.tableView.layer.masksToBounds = true;
    self.tableView.layer.cornerRadius = 10;
    self.tableView.backgroundColor = [UIColor systemGroupedBackgroundColor];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:addBtn];
    [self.view addSubview:modifyBtn];
    [self.view addSubview:deleteBtn];
    [self.view addSubview:queryBtn];
    self.tableView.backgroundView = self.noDataTipLabel;
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = [UIColor darkGrayColor];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    // 設置佈局適配
    int btnWidth = 70, btnHeight = 30, gapXY = 20;
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(gapXY*2);
        make.right.equalTo(self.view.mas_centerX).with.offset(-gapXY/2);
        make.width.mas_equalTo(btnWidth);
        make.height.mas_equalTo(btnHeight);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(gapXY*2);
        make.left.equalTo(self.view.mas_centerX).with.offset(gapXY/2);
        make.width.mas_equalTo(btnWidth);
        make.height.mas_equalTo(btnHeight);
    }];
    
    [self.modifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addBtn.mas_bottom).with.offset(gapXY);
        make.right.equalTo(self.view.mas_centerX).with.offset(-gapXY/2);
        make.width.mas_equalTo(btnWidth);
        make.height.mas_equalTo(btnHeight);
    }];
    
    [self.queryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.deleteBtn.mas_bottom).with.offset(gapXY);
        make.left.equalTo(self.view.mas_centerX).with.offset(gapXY/2);
        make.width.mas_equalTo(btnWidth);
        make.height.mas_equalTo(btnHeight);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.modifyBtn.mas_bottom).with.offset(gapXY);
        make.left.mas_equalTo(gapXY);
        make.right.mas_equalTo(-gapXY);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-gapXY);
    }];
    
    [self.noDataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.tableView.mas_centerX);
        make.centerY.mas_equalTo(self.tableView.mas_centerY);
        make.width.mas_equalTo(self.tableView.mas_width);
        make.height.mas_equalTo(btnHeight);
    }];
}

// MARK: - selector action funcation
// MARK: 增加按鈕響應
- (void)addBtnClick:(UIButton *)sender {
    User *user = [User new];
    user.userId = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
    user.username = [NSString stringWithFormat:@"張三%ld", random()];
    user.age = 18;
    user.createTime = [NSDate date];
    user.income = [[NSDecimalNumber alloc] initWithFloat:1233.04338];
    user.isMan = YES;
    user.userIcon = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573042496310&di=d6b4363fe0bb7455d97672b4d5d0e316&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D4232323981%2C903032036%26fm%3D214%26gp%3D0.jpg";

    [[CZFFMDBExecute shareInstance] addUser:user];

    // 查詢
    [self queryBtnClick:nil];
}

// MARK: 刪除按鈕響應
- (void)deleteBtnClick:(UIButton *)sender {
    if (self.dataSource.count > 0) {
        User *user = self.dataSource[0];
        [[CZFFMDBExecute shareInstance] deleteUser:user.userId];
    }
    
    [self queryBtnClick:nil];
    NSLog(@"刪除成功");
}

// MARK: 需改按鈕響應
- (void)modifyBtnClick:(UIButton *)sender {
    if (self.dataSource.count > 0) {
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:true scrollPosition:UITableViewScrollPositionTop];
        [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
}

// MARK: 查詢按鈕響應
- (void)queryBtnClick:(UIButton *)sender {
    [self.dataSource removeAllObjects];
    
    NSArray *userList = [[CZFFMDBExecute shareInstance] getUserList];
    if (userList.count > 0) {
        [self.dataSource addObjectsFromArray:userList];
    }

    [self.tableView reloadData];
}


// MARK: - UITableView Delegate
// MARK: section Count
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// MARK: row count in section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.noDataTipLabel.hidden = self.dataSource.count > 0;
    return self.dataSource.count;
}

// MARK: cell content
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.detailTextLabel.numberOfLines = 0;
    }
    
    // 填充數據
    User *user = self.dataSource[indexPath.row];
    cell.textLabel.text = user.username;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"age:%d\ncreateTime:%@\nincome:%@\nisMan:%@\nuserId:%@", user.age, user.createTime, user.income, user.isMan ? @"YES":@"NO", user.userId];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:user.userIcon] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (image) {
            // 調整圖片大小
            CGSize imageSize = CGSizeMake(70, 70);
            UIGraphicsBeginImageContextWithOptions(imageSize, NO, UIScreen.mainScreen.scale);
            [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
            cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    }];
    
    return cell;
}

// MARK: cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

// MARK: did selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"請輸入個人信息" preferredStyle:UIAlertControllerStyleAlert];
    
    // selected user
    User *user = self.dataSource[indexPath.row];
    
     //增加確定按鈕;
     [alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         //獲取第1個輸入框;
         UITextField *userNameTextField = alertController.textFields.firstObject;
         if (![userNameTextField.text isEqual:@""] && ![userNameTextField.text isEqual:user.username]) {
             user.username = userNameTextField.text;
         }
         
         //獲取第2個輸入框;
         UITextField *ageTextField = [alertController.textFields objectAtIndex:1];
         if (![ageTextField.text isEqual:@""] && ![ageTextField.text isEqual:[NSString stringWithFormat:@"%d", user.age]]) {
             user.age = [ageTextField.text intValue];
         }
         
         //獲取第3個輸入框;
         UITextField *sexField = alertController.textFields.lastObject;
         if (![sexField.text isEqual:@""]) {
             user.isMan = ![sexField.text isEqual:@"0"];
         }
         [[CZFFMDBExecute shareInstance] updateUser:user];
         
         [self.tableView reloadData];
     }]];
    // 增加取消按鈕
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
        [self.tableView deselectRowAtIndexPath:indexPath animated:true];
    }]];
    
    //定義第一個輸入框;
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
      textField.placeholder = @"請輸入用戶名";
    }];
    //定義第二個輸入框;
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
      textField.placeholder = @"請輸入年齡";
    }];
    //定義第三個輸入框;
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
      textField.placeholder = @"性別";
    }];
    [self presentViewController:alertController animated:true completion:nil];
    
}

// MARK: - get / set 方法

/// get dataSource
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

/// get tableview
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    }
    
    return _tableView;
}


@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章