FMDB增刪改查

ilmari發表於2017-12-15
//
//  ViewController.m
//  FMDBDemo
//
//  Created by Mr_Jia on 2017/4/21.
//  Copyright © 2017年 Mr_Jia. All rights reserved.
//

#import "ViewController.h"
#import <FMDB.h>
#import <ReactiveCocoa.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"demo.sqlite"];
    NSLog(@"DBPATH:%@",dbPath);
    FMDatabase *db = [FMDatabase databaseWithPath: dbPath];
    //設定快取,提升效率
//    [db setShouldCacheStatements:YES];
    UIButton *openDB = [UIButton buttonWithType:UIButtonTypeCustom];
    openDB.backgroundColor = [UIColor grayColor];
    openDB.frame = CGRectMake(100, 50, 200, 50);
    [openDB setTitle:@"OpenDB" forState:UIControlStateNormal];
    [self.view addSubview:openDB];

    [[openDB rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSLog(@"開啟資料庫成功");
        }else{
            NSLog(@"開啟資料庫失敗");
        }
        
    }];
    
    UIButton *creatTable = [UIButton buttonWithType:UIButtonTypeCustom];
    creatTable.backgroundColor = [UIColor grayColor];
    creatTable.frame = CGRectMake(100, 120, 200, 50);
    [creatTable setTitle:@"CreateTable" forState:UIControlStateNormal];
    [self.view addSubview:creatTable];
    
    [[creatTable rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            BOOL result = [db executeUpdate:@"create table if not exists shopCar(dbid integer PRIMARY KEY AUTOINCREMENT,specId integer NOT NULL, shopNum integer NOT NULL default 1, shopInfo TEXT NOT NULL)"];
            if (result) {
                NSLog(@"建立表成功");
            }else{
                NSLog(@"建立表失敗");
            }
            [db close];
        }
    }];
    
    UIButton *insertData = [UIButton buttonWithType:UIButtonTypeCustom];
    insertData.backgroundColor = [UIColor grayColor];
    insertData.frame = CGRectMake(100, 190, 200, 50);
    [insertData setTitle:@"insertData" forState:UIControlStateNormal];
    [self.view addSubview:insertData];
    
    [[insertData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString *sql = [NSString stringWithFormat:@"INSERT INTO shopCar (specId,shopNum,shopInfo) VALUES (%d,%d,'%@')",1,2,@"buzhidaojiale"];
            BOOL result = [db executeUpdate:sql];
            if (result) {
                NSLog(@"插入成功");
            }else{
                
                NSLog(@"插入失敗");
            }
            [db close];
        }
    }];
    
    UIButton *selectAllData = [UIButton buttonWithType:UIButtonTypeCustom];
    selectAllData.backgroundColor = [UIColor grayColor];
    selectAllData.frame = CGRectMake(100, 260, 200, 50);
    [selectAllData setTitle:@"selectAllData" forState:UIControlStateNormal];
    [self.view addSubview:selectAllData];
    
    [[selectAllData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString * sql = @"SELECT * FROM shopCar";
            FMResultSet * rs = [db executeQuery:sql];
            while ([rs next]) {
                int dbId = [rs intForColumn:@"dbid"];
                int specId = [rs intForColumn:@"specId"];
                int shopNum = [rs intForColumn:@"shopNum"];
                NSString * shopInfo = [rs stringForColumn:@"shopInfo"];
                NSLog(@"dbId = %d, specId = %d, shopNum = %d  shopInfo = %@", dbId, specId, shopNum, shopInfo);
            }
            [db close];

        }
    }];
    UIButton *selectDbidData = [UIButton buttonWithType:UIButtonTypeCustom];
    selectDbidData.backgroundColor = [UIColor grayColor];
    selectDbidData.frame = CGRectMake(100, 330, 200, 50);
    [selectDbidData setTitle:@"selectDbidData" forState:UIControlStateNormal];
    [self.view addSubview:selectDbidData];
    
    [[selectDbidData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString * sql = @"SELECT * FROM shopCar WHERE dbid = '1'";
            FMResultSet * rs = [db executeQuery:sql];
            while ([rs next]) {
                int dbId = [rs intForColumn:@"dbid"];
                int specId = [rs intForColumn:@"specId"];
                int shopNum = [rs intForColumn:@"shopNum"];
                NSString * shopInfo = [rs stringForColumn:@"shopInfo"];
                NSLog(@"dbId = %d, specId = %d, shopNum = %d  shopInfo = %@", dbId, specId, shopNum, shopInfo);
            }
            [db close];
            
        }
    }];

    UIButton *updataDbidData = [UIButton buttonWithType:UIButtonTypeCustom];
    updataDbidData.backgroundColor = [UIColor grayColor];
    updataDbidData.frame = CGRectMake(100, 400, 200, 50);
    [updataDbidData setTitle:@"updataDbidData" forState:UIControlStateNormal];
    [self.view addSubview:updataDbidData];
    
    [[updataDbidData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString *updateSql = [NSString stringWithFormat:@"update shopCar set shopNum = '88' where dbid ='1'"];
            BOOL res = [db executeUpdate:updateSql];
            if (res) {
                NSLog(@"修改資料成功");
            } else {
                NSLog(@"修改資料失敗");
            }
            [db close];
            
        }
    }];


    UIButton *deleteDbidData = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteDbidData.backgroundColor = [UIColor grayColor];
    deleteDbidData.frame = CGRectMake(100, 470, 200, 50);
    [deleteDbidData setTitle:@"deleteDbidData" forState:UIControlStateNormal];
    [self.view addSubview:deleteDbidData];
    
    [[deleteDbidData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString *updateSql = [NSString stringWithFormat:@"update shopCar set shopNum = '88' where dbid ='1'"];
            BOOL res = [db executeUpdate:updateSql];
            if (res) {
                NSLog(@"修改資料成功");
            } else {
                NSLog(@"修改資料失敗");
            }
            [db close];
            
        }
    }];

    UIButton *deleteAllData = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteAllData.backgroundColor = [UIColor grayColor];
    deleteAllData.frame = CGRectMake(100, 540, 200, 50);
    [deleteAllData setTitle:@"deleteShopCarTable" forState:UIControlStateNormal];
    [self.view addSubview:deleteAllData];
    
    [[deleteAllData rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            //清空資料表並將自增欄位清零
            NSString *deleteSql = @"DELETE FROM shopCar";
            [db executeUpdate:@"UPDATE sqlite_sequence set seq = 0 where name = 'shopCar'"];
            BOOL res = [db executeUpdate:deleteSql];
            if (res) {
                NSLog(@"清空shopCar表資料成功");
            } else {
                NSLog(@"清空shopCar表資料失敗");
            }
       }
    }];

    
    UIButton *deleteShopCarTable = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteShopCarTable.backgroundColor = [UIColor grayColor];
    deleteShopCarTable.frame = CGRectMake(100, 610, 200, 50);
    [deleteShopCarTable setTitle:@"deleteShopCarTable" forState:UIControlStateNormal];
    [self.view addSubview:deleteShopCarTable];
    
    [[deleteShopCarTable rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if ([db open]) {
            NSString *deleteSql = @"DROP TABLE shopCar";
            BOOL res = [db executeUpdate:deleteSql];
            
            if (res) {
                NSLog(@"刪除shopCar表成功");
            } else {
                NSLog(@"刪除shopCar表失敗");
            }
            [db close];
        }
    }];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

複製程式碼

DEMO

相關文章