美文网首页
FMDB的简单使用

FMDB的简单使用

作者: 动感新势力fan | 来源:发表于2016-04-21 17:21 被阅读120次

    FMDB的github地址
    https://github.com/ccgus/fmdb

    FMDB有三个主要的类
    FMDatabase
    一个FMDatabase对象就代表一个单独的SQLite数据库
    用来执行SQL语句

    FMResultSet
    使用FMDatabase执行查询后的结果集

    FMDatabaseQueue
    用于在多线程中执行多个查询或更新,它是线程安全的

    1.打开创建数据库

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        // 拼接文件名
        NSString *filePath = [cachePath stringByAppendingPathComponent:@"contact.sqlite"];
        // 创建一个数据库的实例,仅仅在创建一个实例,并会打开数据库
        FMDatabase *db = [FMDatabase databaseWithPath:filePath];
        _db = db;
        // 打开数据库
        BOOL flag = [db open];
        if (flag) {
            NSLog(@"打开成功");
        }else{
            NSLog(@"打开失败");
        }
        
        // 创建数据库表
        // 数据库操作:插入,更新,删除都属于update
        // 参数:sqlite语句
       BOOL flag1 = [db executeUpdate:@"create table if not exists t_contact (id integer primary key autoincrement,name text,phone text);"];
        if (flag1) {
            NSLog(@"创建成功");
        }else{
            NSLog(@"创建失败");
    
        }
    }
    

    2.增

    - (IBAction)insert:(id)sender {
        // ?:表示数据库里面的占位符
       BOOL flag = [_db executeUpdate:@"insert into t_contact (name,phone) values (?,?)",@"oooo",@"21321321"];
        if (flag) {
            NSLog(@"success");
        }else{
             NSLog(@"failure");
        }
        
    }
    

    3.删

    - (IBAction)delete:(id)sender {
        BOOL flag = [_db executeUpdate:@"delete from t_contact;"];
        if (flag) {
            NSLog(@"success");
        }else{
            NSLog(@"failure");
        }
    }
    
    

    4.改

    - (IBAction)update:(id)sender {
        // FMDB?,只能是对象,不能是基本数据类型,如果是int类型,就包装成NSNumber  简单包装@12
        BOOL flag = [_db executeUpdate:@"update t_contact set name = ?",@"abc"];
        if (flag) {
            NSLog(@"success");
        }else{
            NSLog(@"failure");
        }
    }
    

    5.查

    - (IBAction)select:(id)sender {
        
      FMResultSet *result =  [_db executeQuery:@"select * from t_contact"];
        
        // 从结果集里面往下找
        while ([result next]) {
         NSString *name = [result stringForColumn:@"name"];
            NSString *phone = [result stringForColumn:@"phone"];
            NSLog(@"%@--%@",name,phone);
        }
        
    }
    

    6.线程安全

    要一起操作成功的这种的一般要用事务

    FMDatabase这个类是线程不安全的,如果在多个线程中同时使用一个FMDatabase实例,会造成数据混乱等问题

    为了保证线程安全,FMDB提供方便快捷的FMDatabaseQueue类

    FMDatabaseQueue的创建 不需要手动打开了

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        // 拼接文件名
        NSString *filePath = [cachePath stringByAppendingPathComponent:@"user.sqlite"];
        // 创建数据库实例
        FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:filePath];
        _queue = queue;
        
        // 创建数据库表
        // 提供了一个多线程安全的数据库实例
        [queue inDatabase:^(FMDatabase *db) {
            
          BOOL flag =  [db executeUpdate:@"create table if not exists t_user (id integer primary key autoincrement,name text,money integer)"];
            
            if (flag) {
                NSLog(@"success");
            }else{
                NSLog(@"failure");
            }
            
        }];   
    }
    

    增删查都差不多 改的时候使用事务

    - (IBAction)update:(id)sender {
        // update t_user set money = 500 where name = 'a';
        //  update t_user set money = 1000 where name = 'b';
        // a -> b 500 a 500
        // b + 500 = b 1000
        
        
        [_queue inDatabase:^(FMDatabase *db) {
            
            // 开启事务
            [db beginTransaction];
           BOOL flag = [db executeUpdate:@"update t_user set money = ? where name = ?;",@500,@"a"];
            if (flag) {
                NSLog(@"success");
            }else{
                NSLog(@"failure");
                // 回滚
                [db rollback];
            }
            
            BOOL flag1 = [db executeUpdate:@"updat t_user set money = ? where name = ?;",@1000,@"b"];
            if (flag1) {
                NSLog(@"success");
            }else{
                NSLog(@"failure");
                [db rollback];
            }
            
            // 全部操作完成时候再去
            [db commit];
        }];    
    }
    

    相关文章

      网友评论

          本文标题:FMDB的简单使用

          本文链接:https://www.haomeiwen.com/subject/qomtrttx.html