文档章节

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
朝阳
多线程操作FMDB的实践

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

云飞扬v5
2016/06/27
15
0
Angelo/FMDB_runtime

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

Angelo
2015/12/16
0
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

没有更多内容

加载失败,请刷新页面

加载更多

下一页

python3.6 取余运算

python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|。 经过测试可发现,python3.6中取余运算得到的 r 是正整数;...

colinux
18分钟前
1
0
[雪峰磁针石博客]软件测试专家工具包1web测试

web测试 本章主要涉及功能测试、自动化测试(参考: 软件自动化测试初学者忠告) 、接口测试(参考:10分钟学会API测试)、跨浏览器测试、可访问性测试和可用性测试的测试工具列表。 安全测试工具...

python测试开发人工智能安全
今天
3
0
JS:异步 - 面试惨案

为什么会写这篇文章,很明显不符合我的性格的东西,原因是前段时间参与了一个面试,对于很多程序员来说,面试时候多么的鸦雀无声,事后心里就有多么的千军万马。去掉最开始毕业干了一年的Jav...

xmqywx
今天
3
0
Win10 64位系统,PHP 扩展 curl插件

执行:1. 拷贝php安装目录下,libeay32.dll、ssleay32.dll 、 libssh2.dll 到 C:\windows\system32 目录。2. 拷贝php/ext目录下, php_curl.dll 到 C:\windows\system32 目录; 3. p...

放飞E梦想O
今天
1
0
谈谈神秘的ES6——(五)解构赋值【对象篇】

上一节课我们了解了有关数组的解构赋值相关内容,这节课,我们接着,来讲讲对象的解构赋值。 解构不仅可以用于数组,还可以用于对象。 let { foo, bar } = { foo: "aaa", bar: "bbb" };fo...

JandenMa
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部