美文网首页
FMDB的简单使用

FMDB的简单使用

作者: 碧海云天V | 来源:发表于2020-04-15 18:09 被阅读0次

    从网上看了FMDB、SQLite、CoreData的区别之后,最终选择了用FMDB做数据存储,其中最主要的原因是FMDB相对简洁易学。
    FMDB 的地址:FMDB

    核心类

    • FMDatabase
      一个FMDatabase对象就代表一个单独的SQLite数据库,用来执行SQL语句。

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

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

    使用前准备

    1、cocopads 引入 FMDB 库pod 'FMDB'

    2、Xcode导入libsqlite3.0框架

    3、在需要使用FMDB的文件中引用#import <FMDB/FMDB.h>

    开始使用

    NO1:创建并打开数据库

    //获取沙河地址;
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
     //设置数据库名字
    NSString *fileName = [docPath stringByAppendingPathComponent:@"student.sqlite"];
    
    //创建并获取数据库信息,如果表存在,则使用该表,不存在则重新创建
    FMDatabase *fmdb = [FMDatabase databaseWithPath:fileName];
    
    //打开数据库
    [fmdb open];
    
    if ([fmdb isOpen]) {
        NSLog(@"数据库打开成功!");
    }else {
        NSLog(@"数据库打开失败!");
    }
    

    NO2:创建数据库表

    //如果表存在,则使用该表,不存在则重新创建
    BOOL creatTable = [fmdb executeUpdate:@"create table if not exists t_student (id integer primary key autoincrement, name text not null, age integer not null, sex text not null);"];
    if (creatTable) {
        NSLog(@"创建表成功");    
    } else {
        NSLog(@"创建表失败");
    }
    

    NO3:【增】插入数据到表中

    BOOL installData = [fmdb executeUpdate:@"insert into t_student (name, age, sex) values (?, ?, ?)", @"张三", @(18), @"男"];
    if (installData) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
    

    NO4:【删】删除表中的数据

    BOOL deleteData = [fmdb executeUpdate:@"delete from t_student where id = ?", @(3)];
    if (deleteData) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    

    NO5:【改】修改表中的数据

    BOOL updateData = [fmdb executeUpdate:@"update t_student set name = ?, age = ?, sex = ? where id = ?", @"贾玲", @(16), @"女", @(2)];
    if (updateData) {
        NSLog(@"修改成功");
    } else {
        NSLog(@"修改失败");
    }
    

    NO6:【查】查询表中的数据

    //FMResultSet *resultSet = [fmdb executeQuery:@"select * from t_student"];//查询所有
    FMResultSet *resultSet = [fmdb executeQuery:@"select * from t_student where id = ?", @(1)];
    while ([resultSet next]) {
        NSString *name = [resultSet stringForColumn:@"name"];
        int age = [resultSet intForColumn:@"age"];
        NSString *sex = [resultSet stringForColumn:@"sex"];
        
        NSLog(@"姓名:%@, 年龄:%@, 性别:%@", name, sex, @(age));
    }
    

    NO7:删除数据库中的表

    //如果表格存在 则销毁
    BOOL deleteTable = [fmdb executeUpdate:@"drop table if exists t_student"];
    if (deleteTable) {
        NSLog(@"删除表成功");
    } else {
        NSLog(@"删除表失败");
    }
    

    NO8:关闭数据库

    //关闭数据库
    [fmdb close];
    if ([fmdb close]) {
        NSLog(@"数据库关闭成功!");
    }else {
        NSLog(@"数据库关闭失败!");
    }
    

    结束语

    以上这些是关于FMDB的基本使用,如有不对之处烦请指正vast0608@163.com,谢谢!

    相关文章

      网友评论

          本文标题:FMDB的简单使用

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