iOS中原生的SQLite API在进行数据存储的时候 需要使用C语言中的函数 操作比较繁琐
于是出现了一系列将SQLite API进行封装的库 FMDB就是其中一种
FMDB 使用OC封装的 简单易用 并且他对于多线程的并发操作进行了处理 所以线程是安全的 但也因为使用OC封装的所以只能在iOS开发的时候使用 在实现跨平台操作的时候存在局限性
FMDatabase : 一个FMDatabase对象就代表一个单独的SQLite数据库 用来执行SQL语句
FMResultSet :使用FMDatabase执行查询后的结果集
FMDatabaseQueue : 用于在多线程中执行多个查询或更新 他的线程是安全的
首先使用cocoaPods把FMDB导入工程
具体代码编写如下:
#import "ViewController.h"
#import <FMDB.h>
@interface ViewController ()
@property (nonatomic, strong) NSString *filePath;
@property (nonatomic, strong) FMDatabase *dataBase;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *crea = @"create table if not exists t_student(id integer primary key autoincrement not null, name text, age integer not null, sex text not null)";
//建表
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.filePath = [document stringByAppendingPathComponent:@"student.sqlite"];
NSLog(@"%@", self.filePath);
self.dataBase = [FMDatabase databaseWithPath:self.filePath];
if ([self.dataBase open]) {
BOOL result = [self.dataBase executeUpdate:crea];
if (result) {
NSLog(@"建表成功");
}else{
NSLog(@"建表失败");
}
}
[self.dataBase close];
}
#pragma mark - 添加
- (IBAction)insertIntoAction:(id)sender {
//先打开数据库
[self.dataBase open];
NSArray *nameArray = [NSArray arrayWithObjects:@"MBBOy", @"炸天", @"小明", nil];
for (int i = 0; i < nameArray.count; i++) {
NSString *name = [nameArray objectAtIndex:i];
//插入语句
// NSString *insertSpl = @"insert into t_student(name,age,sex) values(?,?,?)";
BOOL result = [self.dataBase executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",name,@"18",@"男"];
if (result) {
NSLog(@"插入成功");
}else {
NSLog(@"插入失败");
}
}
[self.dataBase close];
}
#pragma mark - 更改
- (IBAction)updateActon:(id)sender {
//先打开数据库
[self.dataBase open];
BOOL result = [self.dataBase executeUpdate:@"update t_student set name = ? where name = ?",@"孟令徐",@"MBBOy"];
if (result) {
NSLog(@"更改成功");
}else {
NSLog(@"更改失败");
NSLog(@"result = %d", result);
}
[self.dataBase close];
}
#pragma mark - 删除
- (IBAction)deleteAction:(id)sender {
[self.dataBase open];
BOOL result = [self.dataBase executeUpdate:@"delete from t_student where name = ?", @"孟令徐"];
if (result) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
[self.dataBase close];
}
#pragma mark - 查询
- (IBAction)searchAction:(id)sender {
[self.dataBase open];
FMResultSet *resultSet = [self.dataBase executeQuery:@"select * from t_student"];
//遍历出需要的结果内容
while ([resultSet next]) {
NSString *name = [resultSet objectForColumnName:@"name"];
NSInteger age = [resultSet intForColumn:@"age"];
NSString *sex = [resultSet objectForColumnName:@"sex"];
NSLog(@"name= %@, age = %ld, sex = %@", name, age, sex);
}
[self.dataBase close];
}
#pragma mak - 插入很多学生
- (IBAction)insertManyStudents:(id)sender {
//以队列的形式添加数据是FMDB比较常用的添加方式
//FMDB不想支持多个线程同时操作 所以一般以串行的方式实现相关的操作
//第一步:
[self.dataBase open];
//第二步:
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.filePath];
//标识:记录是否操作成功
__block BOOL isSucced = YES;
//第三步:把所需要的事件打包放在操作队列中
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
//串行队列
isSucced = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",@"隔壁老王",@"38",@"男"] && isSucced;
isSucced = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",@"赵符一",@"438",@"位置"] && isSucced;
isSucced = [db executeUpdate:@"insert into t_student(name,age,sex) values(?,?,?)",@"AJAR",@"12",@"男"] && isSucced;
//如果有错误 就会将它返回
if (!isSucced) {
//block返回的参数rollback进行处理(bool 类型指针)
*rollback = YES;
return ;
}
}];
[self.dataBase close];
}