FMDB详解

来源:互联网 发布:json转换java对象 编辑:程序博客网 时间:2024/05/21 19:24

FMDB的简要说明

1.什么是FMDB

FMDB是iOS平台的SQLite数据库框架

FMDB以OC的方式封装了SQLite的C语言API

2.FMDB的优点

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

3.FMDB的github地址

https://github.com/ccgus/fmdb

4.文件路径有三种情况

具体文件路径
如果不存在会自动创建

空字符串@””
会在临时目录创建一个空的数据库
当FMDatabase连接关闭时,数据库文件也被删除

nil
会创建一个内存中临时数据库,当FMDatabase连接关闭时,数据库会被销毁

5.在FMDB中,除查询以外的所有操作,都称为“更新”

create、drop、insert、update、delete等

二、核心类

FMDB有三个主要的类

(1)FMDatabase

一个FMDatabase对象就代表一个单独的SQLite数据库

用来执行SQL语句

(2)FMResultSet

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

(3)FMDatabaseQueue

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

FMDatabase

1.创建数据库和表

    //创建数据库路径    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Person.sqlite"];    NSLog(@"Path: %@", path);    FMDatabase *db = [FMDatabase databaseWithPath:path];    if (![db open]) {        return;    } else NSLog(@"db 已打开");//    创建表格    BOOL issuccessful;    issuccessful = [db executeUpdate:@"create table if not exists Student (name text, age integer primary, sex text, school text, class text, address text)"];    NSLog(@"%@", issuccessful?@"创建表格成功":@"创建表格失败");

2.插入

   //插入一    BOOL isSuccessful;    isSuccessful = [db executeUpdate:@"insert into Student2 values ('YaoMing', 30, 'Male', 'SofeWare 1', 'SofeWare', '上下九步行街')"];    NSError *error = nil;    if (isSuccessful) {        NSLog(@"插入YaoMing成功");    }else NSLog(@"插入YaoMing失败 :%@",error);    //插入二    isSuccessful = [db executeUpdate:@"insert into Student2 (name, sex , age, address) values (?, ?, ?, ?)" values:@[@"韦德", @"男", @34, @"迈阿密热火"] error:&error];    if (isSuccessful) {        NSLog(@"插入成功");    }else NSLog(@"插入韦德失败 :%@",error);    //插入三    isSuccessful = [db executeUpdate:@"insert into Student (name, age, address) values (?, ?, ?)", @"James", @1211, @"美国"];    if (isSuccessful) {        NSLog(@"插入James成功");    }else NSLog(@"插入James失败");

3.

        // 1.执行查询语句         FMResultSet *resultSet = [db executeQuery:@"SELECT * FROM Student"];         // 2.遍历结果         while ([resultSet next]) {                 NSString *address = [resultSet stringForColumn:@"address"];                 NSString *name = [resultSet stringForColumn:@"name"];                 int age = [resultSet intForColumn:@"age"];                 NSLog(@"%@ %@ %d", address, name, age);             }

4.del删除

   int ret = [db executeUpdate:@"DROP TABLE IF EXISTS Student"];    if (ret) {        NSLog(@"del成功");    }else NSLog(@"del失败");

FMDatabaseQueue

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

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

    //FMDatabaseQueue的创建    FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];    [queue inDatabase:^(FMDatabase *db) {        [db executeUpdate:@"INSERT INTO Student(name) VALUES (?)", @"Jack"];        [db executeUpdate:@"INSERT INTO Student(name) VALUES (?)", @"Rose"];        [db executeUpdate:@"INSERT INTO Stundent(name) VALUES (?)", @"Jim"];        FMResultSet *rs = [db executeQuery:@"select * from Student"];        while ([rs next]) {            //...        }    }];
0 0
原创粉丝点击