FMDB使用介绍iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便。于是,就出现了一系列将SQLite API进行封装的库FMDB (https://github.com/ccgus/fmdb) 是一款简洁、易用的封装库,这一篇文章简单介绍下FMDB的使用。
一、简单说明
- 什么是FMDBFMDB是iOS平台的SQLite数据库框架FMDB以OC的方式封装了SQLite的C语言API
- FMDB的优点使用起来更加面向对象,省去了很多麻烦、冗余的c语言代码对比苹果自带的Core Data框架,更加轻量级和灵活提供了多线程安全的数据库操作方法,有效地防止数据混乱
FMDB PK Sqlite优点:对多线程的并发操作进行处理,所以是线程安全的;以OC的方式封装了SQLite的C语言API,使用起来更加的方便;FMDB是轻量级的框架,使用灵活。
缺点:因为它是OC的语言封装的,只能在ios开发的时候使用,所以在实现跨平台操作的时候存在局限性。 - FMDB的github地址https://github.com/ccgus/fmdb
二、核心类FMDB有三个主要的类
(1)FMDatabase一个FMDatabase对象就代表一个单独的SQLite数据库用来执行SQL语句
(2)FMResultSet使用FMDatabase执行查询后的结果集
(3)FMDatabaseQueue用于在多线程中执行多个查询或更新,它是线程安全的
这篇文章 就简单为大家介绍一下FMDB的使用
一、 我们先从以上的连接中 下载FMDB 并将其拖拽到我们所创建的工程中,并导入 libsqlite3.0.tbd。配置好我们的开发环境。
二、创建我们所需要用到的视图,使用MVC模式将其分类,具体效果如下图所示。
在以上所创建的所有视图中 Model所继承的类是NSObject;MyTableViewCell所继承的类是 UITableViewCell;Controller中所有的类都继承于UIViewController;LoadData所继承的类是NSObject。
三、 具体的代码
1. 先在Model.h中写一下的代码
#import <Foundation/Foundation.h>
@interface Model : NSObject
// 自定义属性
// 邮箱
@property(nonatomic,copy)NSString *fieldMail;
// 收货地址
@property(nonatomic,copy)NSString *fieldAddress;
// 收货人
@property(nonatomic,copy)NSString *fieldConsignee;
// 电话
@property (nonatomic , copy) NSString *fieldPhone;
// 备注
@property (nonatomic , copy) NSString *fieldNote;
//
@property (nonatomic , assign) NSInteger ID;
- 在LoadData.h中书写
#import <Foundation/Foundation.h>
// 导入头文件
#import "Model.h"
#import "FMDatabase.h"
@interface LoadData : NSObject
// 单例
+(instancetype)shareLoadData;
// 添加
-(void)AddshareLoadData:(Model *)model;
// 查
-(NSMutableArray *)Marr;
// 删
-(void)deleteLoadData:(Model *)model;
// 改
-(void)UPshareLoadData:(Model *)model;
接着在LoadData.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"];
NSLog(@"path == %@",path);
// 初始化
fate = [[FMDatabase alloc] initWithPath:path];
if ([fate open])
{
// 初始化
[fate executeUpdate:@"create table class (ID integer primary key, fieldMail text, fieldAddress text, fieldConsignee text, fieldPhone text, fieldNote text)"];
[fate close];
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
}
// 添加
-(void)AddshareLoadData:(Model *)model
{
//开始
[fate open];
//初始化
NSString *str = [NSString stringWithFormat:@"insert into class values (null , '%@','%@','%@','%@','%@')",model.fieldMail,model.fieldAddress,model.fieldConsignee,model.fieldPhone,model.fieldNote];
//BOOL值接受
BOOL ii = [fate executeUpdate:str];
//判断
if (ii) {
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.fieldMail = [Set stringForColumn:@"fieldMail"];
mm.fieldAddress = [Set stringForColumn:@"fieldAddress"];
mm.fieldConsignee = [Set stringForColumn:@"fieldConsignee"];
mm.fieldPhone = [Set stringForColumn:@"fieldPhone"];
mm.fieldNote = [Set stringForColumn:@"fieldNote"];
mm.ID = [Set intForColumn:@"ID"];
//添加到数组
[marr addObject:mm];
}
//关闭
[fate close];
//返回值
return marr;
}
// 删
-(void)deleteLoadData:(Model *)model
{
// 开始
[fate open];
// 初始化
NSString *str = [NSString stringWithFormat:@"delete from class where ID = '%ld' ",model.ID];
// BOOL 接受
BOOL ii = [fate executeUpdate:str];
// 判断
if (ii)
{
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
// 关闭
[fate close];
}
// 改
-(void)UPshareLoadData:(Model *)model
{
// 开始
[fate open];
NSString *str = [NSString stringWithFormat:@"update class set fieldMail = '%@',fieldAddress = '%@',fieldConsignee = '%@',fieldPhone = '%@',fieldNote = '%@' where ID = '%ld'",model.fieldMail,model.fieldAddress,model.fieldConsignee,model.fieldPhone,model.fieldNote,model.ID];
// BOOL 接受
BOOL ii = [fate executeUpdate:str];
// 判断
if (ii)
{
NSLog(@"成功");
}
else
{
NSLog(@"失败");
}
// 失败
[fate close];
}
- 接着 我们在MyTableViewCell 中 书写 如下的代码
.h文件
// 邮件
@property(nonatomic,strong) UILabel *fieldMail;
// 收货地址
@property(nonatomic,strong) UILabel *fieldAddress;
// 收货人
@property(nonatomic,strong) UILabel *fieldConsignee;
// 电话
@property(nonatomic,strong) UILabel *fieldPhone;
// 备注
@property(nonatomic,strong) UILabel *fieldNote;
@end
.m文件
// 重写父类方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ([super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self.contentView addSubview:self.fieldMail];
[self.contentView addSubview:self.fieldAddress];
[self.contentView addSubview:self.fieldConsignee];
[self.contentView addSubview:self.fieldPhone];
[self.contentView addSubview:self.fieldNote];
}
return self;
}
//懒加载-//邮件
-(UILabel *)fieldMail{
//判断
if (!_fieldMail) {
//初始化
_fieldMail = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 80, 44)];
}
//返回值
return _fieldMail;
}
//懒加载-//收货地址
-(UILabel *)fieldAddress{
//判断
if (!_fieldAddress) {
//初始化
_fieldAddress = [[UILabel alloc]initWithFrame:CGRectMake(80, 5, 80, 44)];
}
//返回值
return _fieldAddress;
}
//懒加载=//收货人
-(UILabel *)fieldConsignee{
//判断
if (!_fieldConsignee) {
//初始化
_fieldConsignee = [[UILabel alloc]initWithFrame:CGRectMake(155, 5, 80, 44)];
}
//返回值
return _fieldConsignee;
}
//懒加载--//电话
-(UILabel *)fieldPhone{
//判断
if (!_fieldPhone) {
//初始化
_fieldPhone = [[UILabel alloc]initWithFrame:CGRectMake(220, 5, 80, 44)];
}
//返回值
return _fieldPhone;
}
//懒加载
-(UILabel *)fieldNote{
//判断
if (!_fieldNote) {
//初始化
_fieldNote = [[UILabel alloc]initWithFrame:CGRectMake(305, 5, 80, 44)];
}
//返回值
return _fieldNote;
}
4、在viewController文件中实现代码功能
.m文件
#import "ViewController.h"
// 业务处理SQlite
#import "LoadData.h"
// 保存数据
#import "Model.h"
#import "MyTableViewCell.h"
#import "AddViewController.h"
#import "UpViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *marr;
UITableView *tabele;
}
@end
@implementation ViewController
//将要显示
-(void)viewWillAppear:(BOOL)animated
{
//查询
marr = [[LoadData shareLoadData]Marr];
//刷新
[tabele reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//定义标题
self.title = @"信息查询系统";
//初始化
tabele = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//添加协议
tabele.delegate = self;
tabele.dataSource = self;
//添加到试图上
[self.view addSubview:tabele];
//定义按钮
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
//添加到导航调试
self.navigationItem.rightBarButtonItem = right;
}
-(void)click{
//初始化
AddViewController *add = [AddViewController new];
//跳转
[self.navigationController pushViewController:add animated:YES];
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return marr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
//复用池
if (!cell) {
//初始化‘
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
}
//初始化
Model *mm = marr[indexPath.row];
//添加到表格上
cell.fieldMail.text = mm.fieldMail;
cell.fieldAddress.text = mm.fieldAddress;
cell.fieldConsignee.text = mm.fieldConsignee;
cell.fieldPhone.text = mm.fieldPhone;
cell.fieldNote.text = mm.fieldNote;
//返回值
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加
Model *mm = marr[indexPath.row];
// 删除
[[LoadData shareLoadData]deleteLoadData:mm];
[marr removeObjectAtIndex:indexPath.row];
// 刷新
[tabele reloadData];
}
//跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化
UpViewController *up = [UpViewController new];
//添加
up.mm = marr[indexPath.row];
//修改
[[LoadData shareLoadData]UPshareLoadData:up.mm];
//跳转
[self.navigationController pushViewController:up animated:YES];
}
在AddViewController.xib中拖拽一些我们需要的控件,并将其与AddViewController.m关联,具体效果如下图所示,这是我们添加功能的实现
具体的代码如下所示
.m文件
#import "AddViewController.h"
#import "Model.h"
#import "LoadData.h"
@interface AddViewController ()
// 邮件
@property (weak, nonatomic) IBOutlet UITextField *fieldMail;
// 地址
@property (weak, nonatomic) IBOutlet UITextField *fieldAddress;
// 收货人
@property (weak, nonatomic) IBOutlet UITextField *fieldConsignee;
// 电话
@property (weak, nonatomic) IBOutlet UITextField *fieldPhone;
// 备注
@property (weak, nonatomic) IBOutlet UITextField *fieldNote;
- (IBAction)button:(id)sender;
@end
@implementation AddViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)button:(id)sender
{
//初始化
Model *mm = [Model new];
//链接
mm.fieldMail = self.fieldMail.text;
mm.fieldAddress = self.fieldAddress.text;
mm.fieldConsignee = self.fieldConsignee.text;
mm.fieldPhone = self.fieldPhone.text;
mm.fieldNote = self.fieldNote.text;
//添加 到数据库
[[LoadData shareLoadData]AddshareLoadData:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}
接着我们要实现数据库的修改功能,同样先是在UpviewController.xib拖拽一些控件,来实现我们的修改功能
这时 需在.h文件中设置一个属性
#import <UIKit/UIKit.h>
@class Model;
@interface UpViewController : UIViewController
// 属性
@property(nonatomic,strong)Model *mm;
@end
在.m文件中实现我们的具体代码
#import "UpViewController.h"
#import "LoadData.h"
#import "Model.h"
@interface UpViewController ()
@property (weak, nonatomic) IBOutlet UITextField *fieldMail;
@property (weak, nonatomic) IBOutlet UITextField *fielAddress;
@property (weak, nonatomic) IBOutlet UITextField *fieldCosinee;
@property (weak, nonatomic) IBOutlet UITextField *fieldPhone;
@property (weak, nonatomic) IBOutlet UITextField *fieldNote;
- (IBAction)button:(id)sender;
@end
@implementation UpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//将数据添加到修改页面
self.fieldMail.text = self.mm.fieldMail;
self.fielAddress.text = self.mm.fieldAddress;
self.fieldCosinee.text = self.mm.fieldConsignee;
self.fieldPhone.text = self.mm.fieldPhone;
self.fieldNote.text = self.mm.fieldNote;
}
- (IBAction)button:(id)sender
{
//初始化
Model *mm = self.mm;
//链接
mm.fieldMail = self.fieldMail.text;
mm.fieldAddress = self.fielAddress.text;
mm.fieldConsignee = self.fieldCosinee.text;
mm.fieldPhone = self.fieldPhone.text;
mm.fieldNote = self.fieldNote.text;
//添加
[[LoadData shareLoadData]UPshareLoadData:mm];
//跳转
[self.navigationController popViewControllerAnimated:YES];
}