文档章节

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]; }

© 著作权归作者所有

共有 人打赏支持
上一篇: coredata 单表
下一篇: 简易的OC录音机
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
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
JQFMDB,操作简单,线程安全,扩展性强

JQFMDB的特性 针对于FMDB的二次封装 线程安全 操作简单, Model和Dictionary直接存储 拓展性强 支持事务操作 不侵入你的任何Model 安装JQFMDB Cocoapods 手动安装 使用方法 创建数据库 单例方...

小爱无疆
2017/03/14
0
0

没有更多内容

加载失败,请刷新页面

加载更多

搜索引擎(Lucene-索引详解)

IndexWriter详解 问题1:索引创建过程完成什么事? 回顾架构图 Lucene索引创建API 图示 Lucene索引创建代码示例 public static void main(String[] args) throws IOException { // 创建...

这很耳东先生
23分钟前
1
0
JavaScript数据结构之栈实例用法

在本篇文章里小编给大家分享了关于JavaScript数据结构之栈实例用法内容,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。如有不足之处,欢迎批评指正。 栈 先来看...

前端攻城老湿
33分钟前
1
0
springboot 使用 redis 管理session

1.pom.xml 引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency><dependency> ......

狼王黄师傅
34分钟前
1
0
Js取小数点后两位四种方法

在本篇文章里小编给大家分享了关于js取小数点后两位四种方法和实例代码,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。如有不足之处,欢迎批评指正。 Javascri...

前端攻城小牛
40分钟前
1
0
js 唤端

h5页面唤起app const appDownload = () => { // 分享- 唤起 - 下载function failed() {window.location = '应用商店的scheme '}function transfer(cb) {window.location.href =......

何祯粮
40分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部