第三方框架----FMDB的使用

以下是SQLite API进行封装的库FMDB的简单使用 :

FMDB框架的下载地址 :https://github.com/ccgus/fmdb

代码如下 :

//
//  ViewController.m
//  FMDB的使用
//
//  Created by mac1 on 15/10/7.
//  Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//

#import "ViewController.h"
#import "FMDB.h"

@interface ViewController ()

//姓名
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

//年龄
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

//"插入记录"按钮
- (IBAction)insertAction;

//"更新记录"按钮
- (IBAction)updateAction:(id)sender;

//"查询数据"按钮
- (IBAction)queryAction:(id)sender;

//数据库
@property (nonatomic,strong)FMDatabase *fmdb;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.打开数据库  如果不存在,则会自动创建数据库
    
    //数据库路径
    NSString *fmdbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/fmdbDatabase.db"];
    NSLog(@"fmdbPath = %@",fmdbPath);
    
    _fmdb = [FMDatabase databaseWithPath:fmdbPath];
    
    BOOL isSuccess = [_fmdb open];
    
    if (isSuccess) {
        NSLog(@"打开或创建数据库成功!");
        
    //2.创建表
    
        /*
         //1.执行查询操作 SELECT
         [db executeQuery:<#(NSString *), ...#>];
         
         //2.执行查询以外的所有操作,INSERT INTO UPDATE DELETE  CREATE
         [db executeUpdate:<#(NSString *), ...#>];
         */
        isSuccess = [_fmdb executeUpdate:@"CREAT TABLE IF NOT EXISTS t_user (id INTEGER NOT NULL PRIMARY KEY,name TEXT, age INTEGER )"];
        if (isSuccess) {
            
            NSLog(@"创建表成功!");
            
        }
    }
}

//插入数据 DML
- (IBAction)insertAction {
    
    //取得插入的数据记录
    NSString *name = _nameTextField.text;
    NSInteger age = [_ageTextField.text integerValue];
    
    //如果一次性需要操作大量的数据,就不要在主线程操作,会阻塞主线程
    //异步往数据库插入数据
    
    NSString *dbfile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/aysnfmdb.db"];
    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:dbfile];
    
    
    //异步方式下 :
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //会自己创建数据库 db
        [queue inDatabase:^(FMDatabase *db) {
            
            BOOL isSuccess =  [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_user (id integer PRIMARY KEY , name text NOT NULL ,age integer)"];
            
            if (isSuccess) {
                
                NSLog(@"创建表成功");
                
            }
            
            //插入数据
            for (NSInteger i = 0; i < 2000; i++) {
                
                //使用fmdb,sql语句中的字符串不需要写单引号
                BOOL isSuccess = [db executeUpdateWithFormat:@"INSERT INTO t_user(name,age) VALUES (%@,%ld)",name,age];
                if (isSuccess) {
                    NSLog(@"插入数据成功!");
                }
                else
                    NSLog(@"插入数据失败!");
            }
        }];
    });
}

- (IBAction)updateAction:(id)sender {
    

    BOOL issuccess = [_fmdb executeUpdateWithFormat:@"UPDATE t_user SET name = %@ WHERE id = 1",@"大王"];
    if (issuccess) {
        
        NSLog(@"更新数据成功");
    }
    else{
        NSLog(@"更新数据失败");
        
    }

}

- (IBAction)queryAction:(id)sender {
    
    FMResultSet *result = [_fmdb executeQueryWithFormat:@"SELECT * FROM t_user"];
    
    while (result.next) {
        
        // 取出一条数据
        NSString *name = [result stringForColumn:@"name"];
        int age = [result intForColumn:@"age"];
        
        NSLog(@"name = %@  age = %d",name,age);
        
        /*
         
         可以通过下标取数据
        NSString *name = [result stringForColumnIndex:1];
         */
    }
    
}

@end


原文地址:https://www.cnblogs.com/pengsi/p/4859285.html