美文网首页
数据存储-FMDB geekband

数据存储-FMDB geekband

作者: AAup | 来源:发表于2016-04-29 02:28 被阅读74次

    FMDB是轻量级的数据库.在ios中使用十分常见

    FMDB的官方源代码github地址是https://github.com/ccgus/fmdb
    github上基本每一个第三方工具都会有大概介绍它的使用方法,而这个fmdb也不例外。
    它的安装可以使用CocoaPods,在podfile中加入这个.

    FMDB的优点

    使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码
    对比苹果自带的Core Data框架,更加轻量级和灵活
    提供了多线程安全的数据库操作方法,有效地防止数据混乱

    FMDB有3个核心类

    (1)FMDatabase
    一个FMDatabase对象就代表一个单独的SQLite数据库
    用来执行SQL语句
    (2)FMResultSet
    使用FMDatabase执行查询后的结果集
    (3)FMDatabaseQueue
    用于在多线程中执行多个查询或更新,它是线程安全

    用CocoaPods加载完FMDB



    加载完也要添加
    libsqlite3.0.dylib
    首先是设置项目文件,在项目中添加iPhone版的sqlite3的数据库的开发包,在项目下的Frameworks点击右键,然后选择libsqlite3.dylib文件。

    Snip20160428_38.png

    !案例
    #import "ViewController.h"
    #import "FMDB.h"
    @interface ViewController ()
    @property (strong,nonatomic)FMDatabase *db;
    @property BOOL studentTable;
    @end

    @implementation ViewController
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    //1要学会生成文件并进入数据库
    //2学会插入数据
    //3学会查询数据
    //4学会删除数据
    
    //1获取数据库文件路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"student.sqlite"];
    //获得数据库
    FMDatabase *db = [FMDatabase databaseWithPath:path];
    //创建表格
    if ([db open]) {
    //        NSString *sqlStudentTable =  [NSString stringWithFormat:@"create table if not exists iOS_students (student_id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL )"];
    //        BOOL studentTable = [db executeUpdate:sqlStudentTable];
        _studentTable = [db executeUpdate:[NSString stringWithFormat:@"create table if not exists iOS_students (student_id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL )"]];
        if (_studentTable) {
            NSLog(@"创建表格成功");
        }else{
            NSLog(@"不能创建表格");
        }
       
        [db close];
         self.db =db;
    }
    //插入数据
    
    //    //插入方式1;
    //    [_db open];
    //    NSString *insertSpl1 = [NSString stringWithFormat:@"insert into iOS_students (name,age) values ('%@','%@');",@"林拾壹",@"16"];
    //    BOOL spl1 = [_db executeUpdate:insertSpl1];
    //    NSString *insertSpl2 = [NSString stringWithFormat:@"insert into iOS_students (name,age) values ('%@','%@');",@"雯猪猪",@"18"];
    //    BOOL spl2 = [_db executeUpdate:insertSpl2];
    //    
    //    if (_studentTable) {
    //          NSLog(@"创建人物属性成功");
    //    }else{
    //          NSLog(@"创建人物属性失败");
    //    }
        [_db open];
    //    //插入方式2;(含多样化)
    for (int i = 0; i<10; i++) {
         NSString *name = [NSString stringWithFormat:@"临时工-%d", arc4random_uniform(25)];
         // executeUpdate : 不确定的参数用?来占位(最好用对象,直接赋值有弹出黄色狂)
         [self.db executeUpdate:@"INSERT INTO iOS_students (name, age) VALUES (?, ?);", name, @(arc4random_uniform(15)+10)];
    
        // executeUpdateWithFormat : 不确定的参数用%@、%d等来占位
        //        [self.db executeUpdateWithFormat:@"INSERT INTO iOS_students (name, age) VALUES (%@, %d);", name, arc4random_uniform(40)];
    }
    
    
    // 执行查询语句
         FMResultSet *resultSet = [self.db executeQuery:@"SELECT * FROM iOS_students"];
    //遍历
    while ([resultSet next]) {
        int numID = [resultSet intForColumn:@"student_id"];
        NSString *studentName = [resultSet stringForColumn:@"name"];
        int age = [resultSet intForColumn:@"age"];
        NSLog(@"编号=%d人名=%@年龄%d", numID, studentName, age);
    }
    //删除数据
    
    //删除列表内的数据
    //    [self.db executeUpdate:@"DELETE FROM iOS_students;"];
    //删除列表,删除后恢复表格. 等于重新加载
    //    [self.db executeUpdate:@"drop table if exists iOS_students"];
    //    [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS iOS_students (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];//
    
    //删除制定属性数值
            [self.db executeUpdate:@"DELETE FROM iOS_students where student_id <4"];
    
    }
    
    
    @end
    
    Snip20160429_50.png
    源文件 :https://github.com/a000khf/HelloFMDB

    相关文章

      网友评论

          本文标题:数据存储-FMDB geekband

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