iOS-FMDB数据库
2018-01-19 09:39
253 查看
ViewController.m
AddViewController.m
UpViewController.h
UpViewController.m
Book.h继承NSObject
FMDataBaseC.h
FMDataBaseC.m
#import "FMDataBaseC.h" #import "AddViewController.h" #import "UpViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> { UITableView *theTableView; NSMutableArray *arr; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建表格 theTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:theTableView]; theTableView.delegate = self; theTableView.dataSource = self; //右按钮 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(add)]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; arr = [[FMDataBaseC shareData] getAll]; //刷新表格 [theTableView reloadData]; } //添加 -(void)add { AddViewController *add = [[AddViewController alloc] init]; [self.navigationController pushViewController:add animated:YES]; } //表格行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return arr.count; } //表格单元格内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } Book *boo = arr[indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"书名:%@\n备注:%@",boo.nameBook,boo.beiZhu]; cell.textLabel.numberOfLines = 0; return cell; } //表格行高 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 150; } //修改 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UpViewController *up = [[UpViewController alloc] init]; up.boo = arr[indexPath.row]; [self.navigationController pushViewController:up animated:YES]; } //删除 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { Book *boo = arr[indexPath.row]; [[FMDataBaseC shareData] deleteData:boo]; [arr removeObjectAtIndex:indexPath.row]; [theTableView reloadData]; }
AddViewController.m
#import "FMDataBaseC.h" @interface AddViewController () @property (strong, nonatomic) IBOutlet UITextField *nameBookTF; @property (strong, nonatomic) IBOutlet UITextField *nameTF; @property (strong, nonatomic) IBOutlet UITextField *fenLeiTF; @property (strong, nonatomic) IBOutlet UITextField *priceTF; @property (strong, nonatomic) IBOutlet UITextField *beiZhuTF; @end @implementation AddViewController - (void)viewDidLoad { [super viewDidLoad]; } //添加 - (IBAction)addBtn:(id)sender { Book *boo = [[Book alloc] init]; boo.nameBook = self.nameBookTF.text; boo.name = self.nameTF.text; boo.fenLei = self.fenLeiTF.text; boo.price = self.priceTF.text; boo.beiZhu = self.beiZhuTF.text; [[FMDataBaseC shareData] insertData:boo]; [self.navigationController popViewControllerAnimated:YES]; }
UpViewController.h
#import "Book.h" @interface UpViewController : UIViewController @property(nonatomic , strong)Book *boo;
UpViewController.m
#import "UpViewController.h" #import "FMDataBaseC.h" @interface UpViewController () @property (strong, nonatomic) IBOutlet UITextField *nameBookTF; @property (strong, nonatomic) IBOutlet UITextField *nameTF; @property (strong, nonatomic) IBOutlet UITextField *fenLeiTF; @property (strong, nonatomic) IBOutlet UITextField *priceTF; @property (strong, nonatomic) IBOutlet cccd UITextField *beiZhuTF; @end @implementation UpViewController - (void)viewDidLoad { [super viewDidLoad]; self.nameBookTF.text = self.boo.nameBook; self.nameTF.text = self.boo.name; self.fenLeiTF.text = self.boo.fenLei; self.priceTF.text = self.boo.price; self.beiZhuTF.text = self.boo.beiZhu; } //修改 - (IBAction)upBtn:(id)sender { Book *boo = [[Book alloc] init]; boo.ids = self.boo.ids; boo.nameBook = self.nameBookTF.text; boo.name = self.nameTF.text; boo.fenLei = self.fenLeiTF.text; boo.price = self.priceTF.text; boo.beiZhu = self.beiZhuTF.text; [[FMDataBaseC shareData] upData:boo]; [self.navigationController popViewControllerAnimated:YES]; }
Book.h继承NSObject
@interface Book : NSObject @property(nonatomic , assign)NSInteger ids; @property(nonatomic , strong)NSString *nameBook,*name,*fenLei,*price,*beiZhu; @end
FMDataBaseC.h
#import <Foundation/Foundation.h> #import "Book.h" #import <FMDatabase.h> #import <FMResultSet.h> @interface FMDataBaseC : NSObject //单例 +(instancetype)shareData; //添加 -(void)insertData:(Book *)book; //删除 -(void)deleteData:(Book *)book; //修改 -(void)upData:(Book *)book; //查询 -(NSMutableArray *)getAll;
FMDataBaseC.m
#import "FMDataBaseC.h" static FMDataBaseC *fm = nil; static FMDatabase *fmdb; @implementation FMDataBaseC //单例 +(instancetype)shareData { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ fm = [[FMDataBaseC alloc] init]; [fm initCre]; }); return fm; } +(instancetype)allocWithZone:(struct _NSZone *)zone { if (!fm) { fm = [super allocWithZone:zone]; } return fm; } -(id)copy{ return self; } -(id)mutableCopy{ return self; } //创建数据库 -(void)initCre { NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [str stringByAppendingPathComponent:@"BookManagerSy.DB"]; fmdb = [[FMDatabase alloc] initWithPath:path]; if ([fmdb open]) { [fmdb executeUpdate:@"create table BookInfo(ids integer primary key autoincrement, nameBook text, name text, fenLei text, price text, beiZhu text)"]; NSLog(@"创建成功"); [fmdb close]; }else{ NSLog(@"创建失败"); } } //添加 -(void)insertData:(Book *)book { [fmdb open]; BOOL insert = [fmdb executeUpdate:@"insert into BookInfo values(null,?,?,?,?,?)",book.nameBook,book.name,book.fenLei,book.price,book.beiZhu]; if (insert) { NSLog(@"添加成功"); }else{ NSLog(@"添加失败"); } [fmdb close]; } //删除 -(void)deleteData:(Book *)book { [fmdb open]; NSString *str = [NSString stringWithFormat:@"delete from BookInfo where ids = %ld",book.ids]; BOOL delete = [fmdb executeUpdate:str]; if (delete) { NSLog(@"删除成功"); }else{ NSLog(@"删除失败"); } [fmdb close]; } //修改 -(void)upData:(Book *)book { [fmdb open]; NSString *str = [NSString stringWithFormat:@"update BookInfo set nameBook = '%@', name = '%@', fenLei = '%@', price = '%@', beiZhu = '%@' where ids = %ld",book.nameBook,book.name,book.fenLei,book.price,book.beiZhu,book.ids]; BOOL delete = [fmdb executeUpdate:str]; if (delete) { NSLog(@"修改成功"); }else{ NSLog(@"修改失败"); } [fmdb close]; } //查询 -(NSMutableArray *)getAll { [fmdb open]; NSMutableArray *arr = [NSMutableArray array]; FMResultSet *fmset = [[FMResultSet alloc] init]; fmset = [fmdb executeQuery:@"select * from BookInfo"]; while ([fmset next]) { NSInteger ids = [fmset intForColumn:@"ids"]; NSString *nameBook = [fmset stringForColumn:@"nameBook"]; NSString *name = [fmset stringForColumn:@"name"]; NSString *fenLei = [fmset stringForColumn:@"fenLei"]; NSString *price = [fmset stringForColumn:@"price"]; NSString *beiZhu = [fmset stringForColumn:@"beiZhu"]; Book *boo = [[Book alloc] init]; boo.ids = ids; boo.nameBook = nameBook; boo.name = name; boo.fenLei = fenLei; boo.price = price; boo.beiZhu = beiZhu; [arr addObject:boo]; } [fmdb close]; return arr; }
相关文章推荐
- IOS使用FMDB封装的数据库增删改查操作
- iOS数据库第三方框架FMDB学习笔记
- 【iOS】数据库FMDB的使用(二)
- iOS-数据库-FMDB数据库队列
- IOS 数据库(FMDB)
- iOS项目开发中常用到的数据库之FMDB的使用
- iOS学习笔记(十六)——数据库操作(使用FMDB)
- iOS 使用FMDB进行数据库操作
- iOS平台的数据库框架 FMDB
- iOS - 数据库 第三方框架FMDB
- iOS开发数据库篇—FMDB数据库队列
- iOS 使用FMDB进行数据库操作
- iOS中FMDB数据库之增删改查使用实例
- iOS 使用FMDB进行数据库操作
- ios数据库FMDB
- ios数据库之fmdb 使用
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
- [iOS]数据库第三方框架FMDB详细讲解
- iOS关于数据库的操作(使用第三方类库FMDB)