
屏幕快照 2019-01-19 上午8.54.59.png
手動(dòng)導(dǎo)入FMDB
//數(shù)據(jù)庫(kù)必須得有一個(gè)主鍵id
@property(nonatomic,assign)NSInteger classid;
@property(nonatomic,strong)NSString *name,*age,*sex;
FMDBModel.h
#import "ClassMessage.h"
@interface SqlData : NSObject
//單利方法
+(instancetype)initData;
//初始化數(shù)據(jù)庫(kù)
-(void)initSql;
//初始化表格
-(void)initTable;
//添加數(shù)據(jù)
-(void)addData:(ClassMessage *)data;
//修改數(shù)據(jù)
-(void)upData:(ClassMessage *)data;
//刪除數(shù)據(jù)
-(void)deleteData:(NSInteger )theid;
//查詢數(shù)據(jù)
-(NSMutableArray *)array;
//關(guān)閉數(shù)據(jù)庫(kù)
-(void)closeSql;
FMDBModel.m
#import "FMDatabase.h"
#import "ClassMessage.h"
//創(chuàng)建靜態(tài)變量
static SqlData *sqlData1;
static FMDatabase *db;
@implementation SqlData
//單利方法
+ (instancetype)initData{
if (!sqlData1) {
sqlData1 = [[SqlData alloc] init];
}
return sqlData1;
}
//初始化數(shù)據(jù)庫(kù)
- (void)initSql{
//獲取DOcuments目錄
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接路徑
NSString *fileName = [str stringByAppendingString:@"/strname.db"];
//創(chuàng)建數(shù)據(jù)庫(kù)
db = [[FMDatabase alloc] initWithPath:fileName];
if ([db open]) {
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)成功");
//調(diào)用初始化表格方法
[self initTable];
}else{
NSLog(@"數(shù)據(jù)庫(kù)打開(kāi)失敗");
}
}
//初始化表格
-(void)initTable{
//初始化數(shù)據(jù)庫(kù)表格的格式:create table if not exists 表名(主鍵id integer primary key,所有的數(shù)據(jù)類型);
[db executeUpdate:@"create table ClassMessage(classid integer primary key,name text,age text,sex text)"];
//關(guān)閉數(shù)據(jù)庫(kù)
[db close];
}
//添加數(shù)據(jù)
- (void)addData:(ClassMessage *)data{
if ([db open]) {
//添加數(shù)據(jù)的sql語(yǔ)句:insert into 表名 values(null,?,?);
[db executeUpdate:[NSString stringWithFormat:@"insert into ClassMessage values(null,'%@','%@','%@')",data.name,data.age,data.sex]];
}else{
NSLog(@"添加數(shù)據(jù)失敗");
}
//關(guān)閉數(shù)據(jù)庫(kù)
[db close];
}
//刪除數(shù)據(jù)
- (void)deleteData:(NSInteger)theid{
//sql 語(yǔ)句: delete from 表名 where 表名的主鍵id = ?
if ([db open]) {
[db executeUpdate:[NSString stringWithFormat:@"delete from ClassMessage where classid = '%ld'",theid]];
}else{
NSLog(@"刪除數(shù)據(jù)失敗");
}
//關(guān)閉數(shù)據(jù)庫(kù)
[db close];
}
//修改數(shù)據(jù)
- (void)upData:(ClassMessage *)data{
//sql 語(yǔ)句的格式:update 表名 set 所有數(shù)據(jù) where 主鍵id = ?
if ([db open]) {
[db executeUpdate:[NSString stringWithFormat:@"update ClassMessage set name = '%@' ,age = '%@',sex = '%@' where classid = '%ld'",data.name,data.age,data.sex,data.classid]];
}else{
NSLog(@"修改數(shù)據(jù)失敗");
}
//關(guān)閉數(shù)據(jù)庫(kù)
[db close];
}
//查詢數(shù)據(jù)
- (NSMutableArray *)array{
//創(chuàng)建數(shù)據(jù)
NSMutableArray *arr = [NSMutableArray array];
//集合
FMResultSet *set = [FMResultSet new];
if ([db open]) {
//sql 語(yǔ)句格式:select *from 表名
set = [db executeQuery:@"select *from ClassMessage"];
// 判斷有沒(méi)有查詢到 一行一行去查詢
while ([set next]) {
ClassMessage *msg = [[ClassMessage alloc] init];
msg.classid = [set intForColumn:@"classid"];
msg.name = [set stringForColumn:@"name"];
msg.age = [set stringForColumn:@"age"];
msg.sex = [set stringForColumn:@"sex"];
//將msg對(duì)象添加到數(shù)據(jù)
[arr addObject:msg];
}
}else{
NSLog(@"查詢失敗");
}
[db close];
return arr;
}
view.h
@property(nonatomic,strong)UITextField *nameTf,*ageTf,*sexTf;
view.m
//初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.nameTf];
[self addSubview:self.ageTf];
[self addSubview:self.sexTf];
}
return self;
}
-(UITextField *)nameTf{
if (!_nameTf) {
_nameTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 90, self.frame.size.width, 44)];
_nameTf.placeholder = @"please you name";
_nameTf.textAlignment = NSTextAlignmentCenter;
_nameTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _nameTf;
}
-(UITextField *)ageTf{
if (!_ageTf) {
_ageTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 140, self.frame.size.width, 44)];
_ageTf.placeholder = @"please you age";
_ageTf.textAlignment = NSTextAlignmentCenter;
_ageTf.borderStyle = UITextBorderStyleRoundedRect;
}
return _ageTf;
}
-(UITextField *)sexTf{
if (!_sexTf) {
_sexTf = [[UITextField alloc]initWithFrame:CGRectMake(0, 190, self.frame.size.width, 44)];
_sexTf.placeholder = @"please you sex";
_sexTf.textAlignment = NSTextAlignmentCenter;
_sexTf.borderStyle = UITextBorderStyleRoundedRect;
_sexTf.userInteractionEnabled = YES;
}
return _sexTf;
}
viewcontroller.m
#import "SecViewController.h"
#import "SqlData.h"
#import "ClassMessage.h"
@interface ViewController ()
{
NSMutableArray *array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"數(shù)據(jù)庫(kù)";
//右側(cè)按鈕
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click)];
//設(shè)置行高
self.tableView.rowHeight = 100;
}
//視圖即將顯示的時(shí)候調(diào)用
- (void)viewWillAppear:(BOOL)animated{
//調(diào)用數(shù)據(jù)庫(kù)單利方法
[[SqlData initData] initSql];
//調(diào)用查詢數(shù)據(jù)方法
array = [[SqlData initData] array];
//刷新表格
[self.tableView reloadData];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return array.count;
}
//設(shè)置cell內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
//創(chuàng)建classMessage對(duì)象
ClassMessage *classMsg = array[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%ld\n姓名:%@\n年齡:%@\n性別:%@",classMsg.classid,classMsg.name,classMsg.age,classMsg.sex];
cell.textLabel.numberOfLines = 0;
return cell;
}
//點(diǎn)擊右側(cè)按鈕方法
-(void)click{
//跳轉(zhuǎn)到第二個(gè)控制器
SecViewController *secVc = [SecViewController new];
[self.navigationController pushViewController:secVc animated:YES];
}
//點(diǎn)擊單元格方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *secVc = [SecViewController new];
//屬性傳值
secVc.msg = array[indexPath.row];
[self.navigationController pushViewController:secVc animated:YES];
}
//讓表格處于編輯狀態(tài) 刪除數(shù)據(jù)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[[SqlData initData]initSql];
//是刪除主鍵id 獲取數(shù)據(jù)庫(kù)中的數(shù)據(jù)
[[SqlData initData]deleteData:[array[indexPath.row]classid]];
//刪除數(shù)據(jù)
[array removeObject:array[indexPath.row]];
//刷新表格
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
SecViewController.h
#import "ClassMessage.h"
@interface SecViewController : UIViewController
@property(nonatomic,strong)ClassMessage *msg;
SecViewController.m
#import "ClassView.h"
#import "SqlData.h"
@interface SecViewController ()
{
ClassView *theView;
}
@end
@implementation SecViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化classView
theView = [[ClassView alloc] initWithFrame:self.view.frame];
theView.backgroundColor = [UIColor lightGrayColor];
self.view = theView;
theView.nameTf.text = self.msg.name;
theView.ageTf.text = self.msg.age;
theView.sexTf.text = self.msg.sex;
if (!self.msg) {
self.title = @"添加數(shù)據(jù)";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
}else{
self.title = @"修改數(shù)據(jù)";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];
}
}
//點(diǎn)擊save方法 保存數(shù)據(jù)
-(void)save{
ClassMessage *message = [[ClassMessage alloc] init];
message.name = theView.nameTf.text;
message.age = theView.ageTf.text;
message.sex =theView.sexTf.text;
//調(diào)用數(shù)據(jù)庫(kù)方法
[[SqlData initData]initSql];
//調(diào)用添加數(shù)據(jù)方法
[[SqlData initData] addData:message];
//返回上一級(jí)
[self.navigationController popViewControllerAnimated:YES];
}
//點(diǎn)擊edit方法 修改數(shù)據(jù)
-(void)edit{
self.msg.name = theView.nameTf.text;
self.msg.age = theView.ageTf.text;
self.msg.sex = theView.sexTf.text;
//調(diào)用數(shù)據(jù)庫(kù)方法
[[SqlData initData]initSql];
//調(diào)用修改數(shù)據(jù)方法
[[SqlData initData] upData:self.msg];
//返回上一級(jí)
[self.navigationController popViewControllerAnimated:YES];
}