App.m
UIViewController *vc = [[UIViewController alloc]init];
vc.title = @"FMDB";
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
DataBase.h
+(instancetype)initDataBase;
-(void)initData;
-(void)DataTable;
-(void)addData:(ClassRoom *)thaData;
-(void)deleteData:(NSInteger)theID;
-(void)changeData:(ClassRoom *)theData;
-(NSMutableArray *)DataArray;
-(void)closeData;
DataBase.m
static DataBase *theDataBase;
static FMDatabase *db;
+(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;
}
ClassRoom.h
@property(nonatomic,assign)NSInteger integer;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
ClassView.h
@property(nonatomic,strong)UITextField *nameTf,*ageTf;
ClassView.m
-(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;
}
ViewController.m
NSMutableArray *arr;
//添加导航栏
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];
}
SecViewCon.h
@property(nonatomic,strong)ClassRoom *room;
SecViewCon.m
ClassView *classView;
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];
}