文档章节

FMDB 的增删改查

C
 CBETA-陈
发布于 2016/05/16 07:56
字数 1059
阅读 14
收藏 0
#import @interface User : NSObject @property(nonatomic,assign)NSInteger ids; @property(nonatomic,strong)NSString *name; @property(nonatomic,strong)NSString *classes; @property(nonatomic,strong)NSString *grade; @end // #import #import "User.h" @interface FMDBHandle : NSObject //单例 + (instancetype)sharedDataHandle; //增 - (BOOL)insertUser:(User *)user; //删 - (BOOL)delet:(User *)user; //改 - (BOOL)updat:(User *)user; //查询all - (NSMutableArray *)getAll; //查询oneMessage - (User *)getOneMessage:(NSString *)name; @end #import "FMDBHandle.h" #import "FMDatabase.h" //全局静态变量 static FMDatabase *fmdb; static FMDBHandle *dataHandle; @implementation FMDBHandle //单例 + (instancetype)sharedDataHandle{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dataHandle = [[FMDBHandle alloc] init]; [dataHandle initDB]; }); return dataHandle; } + (instancetype)allocWithZone:(struct _NSZone *)zone{ if (!dataHandle) { dataHandle = [super allocWithZone:zone]; } return dataHandle; } - (id)mutableCopy{ return self; } - (id)copy{ return self; } //创建数据库 - (void)initDB{ //沙盒路径 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documentPath stringByAppendingPathComponent:@"user.sqlite"]; //创建数据库 fmdb = [[FMDatabase alloc] initWithPath:path]; if ([fmdb open]) { //创建数据表 [fmdb executeUpdate:@"create table user(ids integer primary key autoincrement, name text, classes text, grade text)"]; [fmdb close]; }else{ NSLog(@"创建失败!"); } } //增 - (BOOL)insertUser:(User *)user{ [fmdb open]; // NSString *sql = [NSString stringWithFormat:@""]; NSLog(@"%@",user.name); BOOL isb = [fmdb executeUpdate:@"insert into user values (null,?,?,?)",user.name,user.classes,user.grade]; if (isb) { NSLog(@"插入成功!"); }else{ NSLog(@"插入失败!"); } [fmdb close]; return isb; } //删 - (BOOL)delet:(User *)user{ [fmdb open]; NSLog(@"%@",user.name); BOOL isb = [fmdb executeUpdate:@"delete from user where name = ?",user.name]; if (isb) { NSLog(@"删除成功!"); }else{ NSLog(@"删除失败!"); } [fmdb close]; return isb; } //改 - (BOOL)updat:(User *)user{ [fmdb open]; NSLog(@"%@",user.name); NSString *sqlStr = [NSString stringWithFormat:@"update user set name = '%@' , classes = '%@' , grade = '%@' where ids = %ld",user.name,user.classes,user.grade,user.ids]; BOOL isb = [fmdb executeUpdate:sqlStr]; if (isb) { NSLog(@"修改成功!"); }else{ NSLog(@"修改失败!"); } [fmdb close]; return isb; } //查询 - (NSMutableArray *)getAll{ [fmdb open]; NSMutableArray *arr = [NSMutableArray array]; FMResultSet *fmset = [fmdb executeQuery:@"select * from user"]; while ([fmset next]) { User *user = [[User alloc] init]; user.ids = [fmset intForColumn:@"ids"]; user.name = [fmset stringForColumn:@"name"]; user.classes = [fmset stringForColumn:@"classes"]; user.grade = [fmset stringForColumn:@"grade"]; NSLog(@"%@",user.classes); [arr addObject:user]; } [fmdb close]; return arr; } //查询oneMessage - (User *)getOneMessage:(NSString *)name{ [fmdb open]; User *user = [[User alloc] init]; FMResultSet *fmset = [fmdb executeQuery:@"select * from user where name = ?",name]; while ([fmset next]) { user.ids = [fmset intForColumn:@"ids"]; user.name = [fmset stringForColumn:@"name"]; user.classes = [fmset stringForColumn:@"classes"]; user.grade = [fmset stringForColumn:@"grade"]; } [fmdb close]; return user; } #import "ViewController.h" #import "InsertViewController.h" #import "User.h" #import "FMDBHandle.h" #import "UpdateViewController.h" @interface ViewController () { UITableView *table; NSMutableArray *arr; } @property (strong, nonatomic) IBOutlet UITextField *nameText; - (IBAction)add:(id)sender; - (IBAction)delet:(id)sender; - (IBAction)selec:(id)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. int width = self.view.frame.size.width; int height = self.view.frame.size.height; table = [[UITableView alloc] initWithFrame:CGRectMake(width/5, height/5*2, width/5*3, height/5*3) style:UITableViewStylePlain]; table.dataSource = self; table.delegate = self; [self.view addSubview:table]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return arr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } User *user = [arr objectAtIndex:indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@",user.name]; cell.detailTextLabel.text = [NSString stringWithFormat:@"班级:%@---成绩:%@",user.classes,user.grade]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UpdateViewController *insert = [mainStoryboard instantiateViewControllerWithIdentifier:@"UpdateViewController"]; insert.user = [arr objectAtIndex:indexPath.row]; [self presentViewController:insert animated:YES completion:nil]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)add:(id)sender { UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; InsertViewController *insert = [mainStoryboard instantiateViewControllerWithIdentifier:@"InsertViewController"]; [self presentViewController:insert animated:YES completion:nil]; } - (IBAction)delet:(id)sender { FMDBHandle *handle = [FMDBHandle sharedDataHandle]; User *user = [[User alloc] init]; user.name = self.nameText.text; BOOL isb = [handle delet:user]; if (isb) { [[[UIAlertView alloc] initWithTitle:@"提示" message:@"删除成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; }else{ [[[UIAlertView alloc] initWithTitle:@"提示" message:@"删除失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; } } - (IBAction)selec:(id)sender { FMDBHandle *handle = [FMDBHandle sharedDataHandle]; if ([self.nameText.text isEqualToString:@""]) { arr = [handle getAll]; }else{ User *user = [handle getOneMessage:self.nameText.text]; arr = [NSMutableArray arrayWithObjects:user, nil]; } [table reloadData]; } #import "InsertViewController.h" #import "FMDBHandle.h" @interface InsertViewController () @property (strong, nonatomic) IBOutlet UITextField *name; @property (strong, nonatomic) IBOutlet UITextField *classes; @property (strong, nonatomic) IBOutlet UITextField *grade; - (IBAction)save:(id)sender; @end @implementation InsertViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } - (IBAction)save:(id)sender { FMDBHandle *handle = [FMDBHandle sharedDataHandle]; User *user = [[User alloc] init]; user.name = self.name.text; user.classes = self.classes.text; user.grade = self.grade.text; BOOL isb = [handle insertUser:user]; if (isb) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [[[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; } } #import #import "User.h" @interface UpdateViewController : UIViewController @property (strong, nonatomic) User *user; @end #import "UpdateViewController.h" #import "FMDBHandle.h" @interface UpdateViewController () @property (strong, nonatomic) IBOutlet UITextField *name; @property (strong, nonatomic) IBOutlet UITextField *classes; @property (strong, nonatomic) IBOutlet UITextField *grade; - (IBAction)updat:(id)sender; @end @implementation UpdateViewController - (void)viewDidLoad { [super viewDidLoad]; self.name.text = self.user.name; NSLog(@"===%@",self.user.classes); self.classes.text = self.user.classes; self.grade.text = self.user.grade; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)updat:(id)sender { FMDBHandle *handle = [FMDBHandle sharedDataHandle]; self.user.name = self.name.text; self.user.classes = self.classes.text; self.user.grade = self.grade.text; NSLog(@"id:%ld",self.user.ids); BOOL isb = [handle updat:self.user]; if (isb) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [[[UIAlertView alloc] initWithTitle:@"提示" message:@"修改失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; }

© 著作权归作者所有

共有 人打赏支持
C
粉丝 0
博文 5
码字总数 3395
作品 0
朝阳
Angelo/FMDB_runtime

#FMDB_runtime /** * 对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * * 基于 FMDB * * 操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整体进行操作 * * 根据 mo...

Angelo
2015/12/16
0
0
多线程操作FMDB的实践

最近在弄项目的时候,需要多线程读写数据库,之前用的SQlite遇到多线程就不给力,很容易出现ACCESS_BAD的问题,于是找了些资料,发现FMDB的FMDBQueue可以比较容易的处理多线程的问题,所以就...

云飞扬v5
2016/06/27
15
0
PeakSqlite

给FMDB再加一层,封装Sqlite常用的增删改查,主要针对单表操作。项目中包括一个TODO的简单示例,包括对sqlite的增删改查操作。 建议结合作者的另一个项目peaksqlite-entity-maker(https://g...

红薯
2013/09/08
293
0
iOS sqlite 增删改查 简单封装(基于 FMDB)

/** * 对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * * 基于 FMDB * * 操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整体进行操作 * * 根据 model 对象自动建表...

别情花如依丶
2016/04/20
7
0
第三方库 FMDB的使用

数据库的使用 总结自:http://www.cnblogs.com/wuhenke/archive/2012/02/07/2341656.html // SQLite的大小写敏感性,不区分带小写。下面的UPDATE 可以写成小写的不过要用双引号引起来。工程里...

水一样的人儿
2016/01/15
30
0

没有更多内容

加载失败,请刷新页面

加载更多

分布式块存储的引擎如何设计?

前言: 目前在万兆网络和SSD,包括NVMe SSD 都已经非常普及。随着硬件的速度越来越快,性能的瓶颈会从硬件转移到软件。尤其对于存储引擎来说,性能至关重要。 先来看一下我们会对数据存储引擎...

Java干货分享
20分钟前
1
0
docker(五):docker-compose.yml 配置

docker-compose.yml常用命令 image 指定镜像名称或者镜像id,如果该镜像在本地不存在,Compose会尝试pull下来。 示例: image: java build 指定Dockerfile文件的路径。可以是一个路径,例如...

开心的哈士奇
24分钟前
1
0
Tale的升级真是惊心动魄,吓死人

精心动魄的博客升级计划,Tale 这几天在筹划将分散在其他几个地方的博客统一到这里来,也就留意了tale的更新,发现出现了2.0.1版本; 因此动了升级的念头,唉! 高估了个人开放着的系统规划能...

硅步积千里
35分钟前
1
0
tcc分布式事物

因为最近公司的新退货系统用到了spring cloud。所以会涉及到一些分布式事物。 所以需要先了解一下,分布式事物。 shuaiqiyu / hmily 高性能异步分布式事务TCC框架 谭纳 / spring-cloud-rest-...

miaojiangmin
35分钟前
1
0
20181016 上课截图

小丑鱼00
41分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部