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"http://數據類頭文件

@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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • App.m UIViewController *vc = [[UIViewController alloc]ini...
    本澤馬閱讀 234評論 0 0
  • 哦吼吼,又研究了幾天,把FMDB這個封裝好的數據庫搞定了,寫了個簡單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 544評論 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 924評論 0 0
  • 用cocoaPods配置第三方文件 第一步。打開終端 第二步。cd+文件夾 第三步。pod init 第四步。打開...
    不說謊的匹諾曹Y閱讀 1,110評論 0 1
  • 首先 導入FMDB 并添加FMDB依賴庫(labslite3.0) 創建model類 如圖 緊接著創建業務處理層...
    酷酷的疼閱讀 508評論 0 0