美文网首页
FMDB简单使用

FMDB简单使用

作者: 冀F旭 | 来源:发表于2016-08-18 15:28 被阅读81次

FMDB简单使用

  • FMDatabase的使用
  • FMDatabaseQueue的使用
  • 使用FMDatabaseQueue主要是为了线程安全,如果没有线程问题请使用FMDatabase,这样性能更优
  • FMDB事务
    示例代码:
@interface ViewController ()
//@property (nonatomic, strong) FMDatabase *db;
@property (nonatomic, strong) FMDatabaseQueue *dbQueue;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqliteFilePath = [path stringByAppendingPathComponent:@"student.sqlite"];
    
    self.dbQueue = [FMDatabaseQueue databaseQueueWithPath:sqliteFilePath];
    
    // 使用FMDatabaseQueue,为了线程安全!!!
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        
        if ([db open])
        {
            // 创建表
            NSString *sql = @"CREATE TABLE IF NOT EXISTS t_student(name TEXT , socre REAL, rank INTEGER , id INTEGER PRIMARY KEY AUTOINCREMENT);";
            if ([db executeUpdate:sql])
            {
                NSLog(@"创建表成功");
            }
            else
            {
                NSLog(@"创建表失败");
            }
        }
        else
        {
            NSLog(@"打开数据库失败");
        }
    }];
    
    
    
    // 使用FMDatabase
    /*
    if ([self.dbQueue open])
    {
        // 创建表
        NSString *sql = @"CREATE TABLE IF NOT EXISTS t_student(name TEXT , socre REAL, rank INTEGER , id INTEGER PRIMARY KEY AUTOINCREMENT);";
        if ([self.dbQueue executeUpdate:sql])
        {
            NSLog(@"创建表成功");
        }
        else
        {
            NSLog(@"创建表失败");
        }
    }
    else
    {
        NSLog(@"打开数据库失败");
    }
     */
    
    // 获取沙盒路径
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask,YES);
    NSString *ourDocumentPath =[documentPaths objectAtIndex:0];
    NSLog(@"%@",ourDocumentPath);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 插入
    /*
    if([self.db executeUpdate:@"INSERT INTO  t_student (name, socre, rank) VALUES(?, ?, ?);", @"jack", @(99.8), @(1)])
    {
        NSLog(@"插入表成功");
    }else
    {
        NSLog(@"插入表失败");
    }
     */
    
    // 查询
    /*
    NSString *sql =  @"SELECT name, socre, rank, id FROM t_student;";
    
    FMResultSet *set = [self.db executeQuery:sql];
    
    while ([set next])
    {
//        NSString *str = [set stringForColumnIndex:0];
//        double socre = [set doubleForColumnIndex:0];
//        int rank = [set intForColumnIndex:0];
        
        NSString *str = [set stringForColumn:@"name"];
        double socre = [set doubleForColumn:@"socre"];
        int rank = [set intForColumn:@"rank"];
        
        NSLog(@"%@  %f  %d", str, socre, rank);
    }
     */
     
     // 使用FMDatabaseQueue
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        
        if([db executeUpdate:@"INSERT INTO  t_student (name, socre, rank) VALUES(?, ?, ?);", @"jack", @(99.8), @(1)])
        {
            NSLog(@"插入表成功");
        }else
        {
            NSLog(@"插入表失败");
        }
    }];
    
    
    // FMDB事务,开启事务后,只有提交了事务才会真正的修改数据库
    [self.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        [db beginTransaction];  // 开启事务
        if ([db executeUpdate:@"UPDATE t_student SET socre = 80 WHERE name = 'jack';"])
        {
            NSLog(@"更新成功");
        }
        else
        {
            NSLog(@"更新失败");
        }
        
        // 制造一个崩溃(银行存取款,断电问题),沙盒临时出现一个student.sqlite-journal文件
        NSArray *array = @[@"123"];
        array[3];
        
        if ([db executeUpdate:@"UPDATE t_student SET socre = 0 WHERE name = 'jack';"])
        {
            NSLog(@"更新成功");
        }
        else
        {
            NSLog(@"更新失败");
        }
        
        [db commit];  // 提交事务
    }];
     
}



@end

相关文章

  • FMDB简单使用

    FMDB简单使用 FMDatabase的使用 FMDatabaseQueue的使用 使用FMDatabaseQue...

  • fmdb简单使用

    1.其实平时简单的使用不过就是增删改改查,我们看一下代码 数据库的创建: 以上所用的语言就是sqlite语言相信用...

  • iOS FMDB增删改查

    FMDB是一款简洁、易用的封装库,下面简单介绍下FMDB的使用: 在FMDB下载文件后,导入工程中,并使用 lib...

  • FMDB常用API简述及多线程单例类封装

    FMDB的简单使用 导入头文件 #import "FMDB.h" 全局的静态数据库对象initialize// 这...

  • FMDB与JKDBModel在工程中使用

    FMDB: 由于iOS系统原生的SQLite API在使用上过于繁琐,FMDB就是将原生的api封装成简单容易使用...

  • 数据库

    数据库常用操作介绍增删改查 FMDB FMDB简单介绍 、FMDB使用 需求:判断一个table是存在 判断一个t...

  • FMDB的简单的使用

    FMDB的简单的使用 首先需要知道的, 就是FMDB拥有的三个大类 FMDatabaseQueue FMDatab...

  • ios FMDB 简单使用

    FMDB 前言. 去年整理的文章了,好久不做ios,再不发,估计ios就废了,拿来记录一下,大牛勿喷 一、FMDB...

  • FMDB的简单使用

    FMDB:一种轻量级的数据库,使用起来非常的方便。其相比于SQLite NSUserDefaults等来说,有很多...

  • FMDB的简单使用

    1.FMDB简单介绍 iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便。于是,就出现了一系...

网友评论

      本文标题:FMDB简单使用

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