#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