FMDB 的增删改查
FMDB 的增删改查
CBETA-陈 发表于2年前
FMDB 的增删改查
  • 发表于 2年前
  • 阅读 14
  • 收藏 0
  • 点赞 2
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: 考试成绩查询系统
#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]; }
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 5
码字总数 3395
×
CBETA-陈
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: