1.FMDB简单介绍
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便。于是,就出现了一系列将SQLite API进行封装的库FMDB (https://github.com/ccgus/fmdb) 是一款简洁、易用的封装库
FMDB同时兼容ARC和非ARC工程,会自动根据工程配置来调整相关的内存管理代码。
FMDB常用类:
FMDatabase : 一个单一的SQLite数据库,用于执行SQL语句。
FMResultSet :执行查询一个FMDatabase结果集。 FMDatabaseQueue :在多个线程来执行查询和更新时会使用这个类。
创建数据库:
db = [FMDatabase databaseWithPath:database_path];
● FMDB优点:
1.使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码
2.对比苹果自带的CoreData框架,更加轻量级和灵活
3.提供多线程安全,有效地防止数据混乱,原来的SQLite不是线程安全的
● FMDB缺点:
因为是OC语言封装的,失去了SQLite原来的跨平台性(相对于其他语言)
2.效果图
3.注意点
1.使用cocoapods
或者去下载FMDBsdk拖进工程中
2.须导入libsqlite3.0.tbd依赖库
4.代码展示
1.创建模型用于接受数据
import "model.h"
.h
//分类
@property(nonatomic,strong) NSString *classify;
//书名
@property(nonatomic,strong) NSString *bookName;
//价格
@property(nonatomic,strong) NSString *bookPrice;
//作者
@property(nonatomic,strong) NSString *bookAuthor;
//备注
@property(nonatomic,strong) NSString *remark;
//id
@property(nonatomic,assign) NSInteger bookID;
2.创建业务处理层用于处理数据
.h
#import "FMDatabase.h"
#import "model.h"
@interface loadData : NSObject
//单例
+(instancetype)shareLoadData;
//添加
-(void)addBook:(model *)mode;
//查询
-(NSMutableArray *)marr;
//修改
-(void)changeBook:(model *)model;
//删除
-(void)deleteBook:(model *)model;
.m
#import "loadData.h"
static loadData *ld =nil;
static FMDatabase *fate;
@implementation loadData
//单例
+(instancetype)shareLoadData
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[loadData alloc]init];
[ld initA];
});
return ld;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!ld) {
ld = [super allocWithZone:zone];
}
return ld;
}
-(id)copy
{
return self;
}
-(id)mutableCopy
{
return self;
}
//初始化数据库
-(void)initA{
//创建沙盒
NSString *Ste = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//定义文件名
NSString *path = [Ste stringByAppendingPathComponent:@"HousingInfo.sqlite"];
//初始化
fate = [[FMDatabase alloc]initWithPath:path];
//判断
if ([fate open]) {
//初始化
[fate executeUpdate:@"create table class (bookID integer primary key, classify text, bookName text , bookPrice text , bookAuthor text , remark text)"];
[fate close];
NSLog(@"成功");
}else{
NSLog(@"失败");
}
}
//添加
-(void)addBook:(model *)mode
{
BOOL isc = false;
NSArray *arr = [self marr];
for (model *m in arr) {
if ([m.classify isEqualToString:mode.classify] && [m.bookName isEqualToString:mode.bookName]&&[m.bookPrice isEqualToString:mode.bookPrice]&&[m.bookAuthor isEqualToString:mode.bookAuthor]&&[m.remark isEqualToString:mode.remark]) {
isc = true;
}
}
if (isc) {
NSLog(@"数据相同");
}else{
[fate open];
BOOL isb = [fate executeUpdate:[NSString stringWithFormat:@"insert into class values (null , '%@','%@','%@','%@','%@')",mode.classify,mode.bookName,mode.bookPrice,mode.bookAuthor,mode.remark]];
if (isb) {
NSLog(@"添加成功");
}else{
NSLog(@"添加失败");
}
[fate close];
}
//查询
-(NSMutableArray *)marr
{
//初始化
NSMutableArray *marr = [NSMutableArray new];
//开始
[fate open];
//初始化
FMResultSet *Set = [[FMResultSet alloc]init];
//使用set接受
Set = [fate executeQuery:@"select *from class"];
//判断
while ([Set next]) {
//初始化
model *mm = [model new];
//链接
mm.classify = [Set stringForColumn:@"classify"];
mm.bookName = [Set stringForColumn:@"bookName"];
mm.bookPrice = [Set stringForColumn:@"bookPrice"];
mm.bookAuthor = [Set stringForColumn:@"bookAuthor"];
mm.remark = [Set stringForColumn:@"remark"];
mm.bookID = [Set intForColumn:@"bookID"];
//添加到数组
[marr addObject:mm];
}
//关闭
[fate close];
//返回值
return marr;
}
//修改
-(void)changeBook:(model *)model
{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"update class set classify = '%@',bookName = '%@',bookPrice = '%@',bookAuthor = '%@',remark = '%@' where bookID = '%ld'",model.classify,model.bookName,model.bookPrice,model.bookAuthor,model.remark,model.bookID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}
//删除
-(void)deleteBook:(model *)model
{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"delete from class where bookID = '%ld' ",model.bookID];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii) {
NSLog(@"成功");
}else{
NSLog(@"失败");
}
//关闭
[fate close];
}
@end
3.数据进行展示
//将要显示
-(void)viewWillAppear:(BOOL)animated{
//查询
Marr = [[loadData shareLoadData]marr];
//刷新
[tabele reloadData ];//这个是展示在表格中的
}
4.数据添加页面
#import "model.h"
#import "loadData.h"
@interface addViewController ()
@property (weak, nonatomic) IBOutlet UITextField *classiftyTF;
@property (weak, nonatomic) IBOutlet UITextField *bookNameTF;
@property (weak, nonatomic) IBOutlet UITextField *bookAuthorTF;
@property (weak, nonatomic) IBOutlet UITextField *bookpriceTF;
@property (weak, nonatomic) IBOutlet UITextField *remarkTF;
@end
@implementation addViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)add:(id)sender {
//初始化
model *mm = [model new];
//链接
mm.classify =self.classiftyTF.text;
mm.bookName =self.bookNameTF.text;
mm.bookPrice =self.bookpriceTF.text;
mm.bookAuthor =self.bookpriceTF.text;
mm.remark =self.remarkTF.text;
//添加到数据库
[[loadData shareLoadData]addBook:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}
5.数据修改界面
从原界面到修改界面有一步传值操作(属性传值)
#import "model.h"
#import "loadData.h"
@interface changeViewController ()
@property (weak, nonatomic) IBOutlet UITextField *classityTF;
@property (weak, nonatomic) IBOutlet UITextField *bookAuthorTF;
@property (weak, nonatomic) IBOutlet UITextField *bookPriceTF;
@property (weak, nonatomic) IBOutlet UITextField *bookNameTF;
@property (weak, nonatomic) IBOutlet UITextField *remarkTF;
@end
@implementation changeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//将数据添加到修改页面
self.classityTF.text =self.mm.classify;
self.bookNameTF.text =self.mm.bookName;
self.bookPriceTF.text =self.mm.bookPrice;
self.bookAuthorTF.text =self.mm.bookAuthor;
self.remarkTF.text =self.mm.remark;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)change:(id)sender {
//初始化
model *mm = self.mm;
//链接
mm.classify = self.classityTF.text;
mm.bookName = self.bookNameTF.text;
mm.bookPrice = self.bookPriceTF.text;
mm.bookAuthor = self.bookAuthorTF.text;
mm.remark = self.remarkTF.text;
//添加
[[loadData shareLoadData]changeBook:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}