FMDB简单使用

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基本使用 1、什么是FMDB? FMDB是一款简洁、易用的封装库。因此,在这里推荐使用第三方框架FMD...
    豆宝的老公阅读 10,335评论 1 14
  • FMDB本质是对SQLit3.0的封装,使用步骤一样 1.创建一个类来管理数据库 2.将该类设计成单例 3.打开或...
    船长_阅读 2,860评论 5 6
  • 第一步:插入libsqlite3.tbd 第二步:viewController.m全部代码 第三步:运行即可。。。
    Hyman0819阅读 559评论 0 0
  • 以下代码适用于直接在代码中操作数据库,实际中可以使用navicat图形化界面先进行数据库测试数据的填充,在APP启...
    karthus阅读 268评论 0 0
  • 1.其实平时简单的使用不过就是增删改改查,我们看一下代码 数据库的创建: 以上所用的语言就是sqlite语言相信用...
    shuaikun阅读 412评论 1 3