FMDB

#import@interface AppDelegate : 

UIResponder@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong) NSPersistentContainer *persistentContainer;

- (void)saveContext;

@end


#import@interface ClassRoom : NSObject

@property(nonatomic,assign)NSInteger intTeger;

@property(nonatomic,strong)NSString  *name,*age;

@end


#import#import//数据库

#import "ClassRoom.h"//数据类头文件

@interface DataBase : NSObject

//单例

+(instancetype)initDataBase;

//初始化数据库

-(void)initData;

//创建数据库表格

-(void)createTable;

//添加数据

-(void)addData:(ClassRoom *)thaData;

//删除数据

-(void)deleteData:(NSInteger)theId;

//修改数据

-(void)changerData:(ClassRoom *)theData;

//查询数据

-(NSMutableArray*)dataArray;

@end


//

//  DataBase.m

//  第七单元 FMDB

//

//  Created by 爱人闵敬凌 on 2017/6/16.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "DataBase.h"

#import "FMDatabase.h"

static DataBase *theDataBase;

static FMDatabase *db;

@implementation DataBase

//单例

+(instancetype)initDataBase{

if (!theDataBase)

{

theDataBase = [[DataBase alloc]init];

}

return theDataBase;

}

//初始化数据库

-(void)initData

{

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

NSString *newPath = [path stringByAppendingString:@"/qq.db"];

db = [[FMDatabase alloc]initWithPath:newPath];

if ([db open])

{

NSLog(@"数据库创建成功");

[self createTable];

}

else

{

NSLog(@"数据库创建失败");

}

}

//创建数据库表+格

-(void)createTable

{

[db executeUpdate:@"create table classroom(intTeger integer primary key,name text,age text)"];

[db close];

}

//添加数据

-(void)addData:(ClassRoom *)thaData{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"insert into classroom values(null,'%@','%@')",thaData.name,thaData.age]];

}

else

{

NSLog(@"添加失败");

}

[db close];

}

//删除数据

-(void)deleteData:(NSInteger)theId

{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"delete from classroom where intTeger = '%ld'",theId]];

}

else

{

NSLog(@"删除失败");

}

[db close];

}

//修改数据

-(void)changerData:(ClassRoom *)theData

{

if ([db open])

{

[db executeUpdate:[NSString stringWithFormat:@"update classroom set name = '%@',age = '%@' where intTeger = '%ld'",theData.name,theData.age,theData.intTeger]];

}

else

{

NSLog(@"需改失败");

}

[db close];

}

//查询数据

-(NSMutableArray*)dataArray

{

NSMutableArray *arr = [NSMutableArray array];

[db open];

FMResultSet *set = [[FMResultSet alloc]init];

set  = [db executeQuery:@"select *from classroom"];

while ([set next]) {

ClassRoom *room = [[ClassRoom alloc]init];

room.intTeger = [set intForColumn:@"intTeger"];

room.name = [set stringForColumn:@"name"];

room.age = [set stringForColumn:@"age"];

[arr addObject:room];

}

[db close];

return arr;

}

@end


#import@interface ClassView : UIView

@property(nonatomic,strong)UITextField *nameTf,*ageTf;

@end


//

//  ClassView.m

//  第七单元 FMDB

//

//  Created by 爱人闵敬凌 on 2017/6/16.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "ClassView.h"

@implementation ClassView

-(instancetype)initWithFrame:(CGRect)frame

{

if (self = [super initWithFrame:frame])

{

[self addSubview:self.nameTf];

[self addSubview:self.ageTf];

}

return self;

}

-(UITextField *)nameTf

{

if (!_nameTf)

{

_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 100, 200, 40)];

_nameTf.borderStyle = UITextBorderStyleRoundedRect;

_nameTf.placeholder = @"please you name";

}

return _nameTf;

}

-(UITextField *)ageTf

{

if (!_ageTf) {

_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(30, 150, 200, 40)];

_ageTf.borderStyle = UITextBorderStyleRoundedRect;

_ageTf.placeholder = @"please you age";

}

return _ageTf;

}

@end


//

//  ViewController.m

//  第七单元 FMDB

//

//  Created by 爱人闵敬凌 on 2017/6/16.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "ViewController.h"

#import "SecViewController.h"

#import "DataBase.h"

#import "ClassRoom.h"

@interface ViewController ()

{

NSMutableArray *arr;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//添加导航栏

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];

}

-(void)viewWillAppear:(BOOL)animated{

[[DataBase initDataBase]initData];

arr = [[DataBase initDataBase]dataArray];

[self.tableView reloadData];

}

//实现点击方法

-(void)click{

SecViewController *add = [[SecViewController alloc]init];

[self.navigationController pushViewController:add animated:YES];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 80;

}

//确定行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return arr.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

ClassRoom *classroom = arr[indexPath.row];

cell.textLabel.text = [NSString stringWithFormat:@"%ld\n%@\n%@",classroom.intTeger,classroom.name,classroom.age];

cell.textLabel.numberOfLines = 0;

return cell;

}

//删除数据

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

ClassRoom * model = arr[indexPath.row];

[[DataBase initDataBase]deleteData:model.intTeger];

[arr removeObjectAtIndex:indexPath.row];

[self.tableView reloadData];

}

//修改数据

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

SecViewController * sec = [[SecViewController alloc]init];

sec.room = arr[indexPath.row];

[self.navigationController pushViewController:sec animated:YES];

}

@end


#import#import "ClassRoom.h"

@interface SecViewController : UIViewController

@property(nonatomic,strong)ClassRoom *room;

@end


//

//  SecViewController.m

//  第七单元 FMDB

//

//  Created by 爱人闵敬凌 on 2017/6/16.

//  Copyright © 2017年 百度. All rights reserved.

//

#import "SecViewController.h"

#import "ClassView.h"

#import "DataBase.h"

#import "ClassRoom.h"

@interface SecViewController ()

{

ClassView *classView;

}

@end

@implementation SecViewController

- (void)viewDidLoad {

[super viewDidLoad];

classView = [[ClassView alloc]initWithFrame:self.view.frame];

classView.backgroundColor = [UIColor whiteColor];

self.view = classView;

classView.nameTf.text = self.room.name;

classView.ageTf.text =  self.room.age;

if (classView.nameTf.text.length<= 0) {

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];

}

else

{

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];

}

}

-(void)save{

ClassRoom *room = [ClassRoom new];

room.name = classView.nameTf.text;

room.age = classView.ageTf.text;

[[DataBase initDataBase]initData];

[[DataBase initDataBase]addData:room];

[self.navigationController popViewControllerAnimated:YES];

}

-(void)edit{

self.room.name = classView.nameTf.text;

self.room.age  = classView.ageTf.text;

[[DataBase initDataBase]initData];

[[DataBase initDataBase]changerData:self.room];

[self.navigationController popViewControllerAnimated:YES];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • App.m UIViewController *vc = [[UIViewController alloc]ini...
    本泽马阅读 214评论 0 0
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 475评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 698评论 0 0
  • 用cocoaPods配置第三方文件 第一步。打开终端 第二步。cd+文件夹 第三步。pod init 第四步。打开...
    不说谎的匹诺曹Y阅读 995评论 0 1
  • 首先 导入FMDB 并添加FMDB依赖库(labslite3.0) 创建model类 如图 紧接着创建业务处理层...
    酷酷的疼阅读 458评论 0 0