FMDB增删改查(成绩查询)

首先

导入FMDB

并添加FMDB依赖库(labslite3.0)

创建model类    如图

FMDB增删改查(成绩查询)_第1张图片
FMDB增删改查(成绩查询)_第2张图片

紧接着创建业务处理层

如图

FMDB增删改查(成绩查询)_第3张图片

代码如下

+(instancetype)shareLoadData;

//创建表

-(void)createTable;

//添加数据

-(void)addData:(Student *)stu;

//修改数据

-(void)updateData:(Student *)stu;

//删除数据

-(void)deleteData:(NSInteger)idl;

//查询数据

-(NSMutableArray*)showAllData;

//查询第一个数据

-(NSMutableArray *)showTop1Data;

//查询理论分数最高分

-(NSMutableArray *)showTheoryMaxData;

//查询技能分数最高分

-(NSMutableArray *)showComputerMaxData;

//删除理论分数最低分

-(NSMutableArray *)deleteTheoryMinData;

FMDB增删改查(成绩查询)_第4张图片

接下来

#import "LoadData.h"

static LoadData *load;

@interface LoadData ()

@property (nonatomic, strong) FMDatabase *db;

@end

@implementation LoadData

+(instancetype)shareLoadData{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

load = [[LoadData alloc] init];

});

return load;

}

+(instancetype)allocWithZone:(struct _NSZone *)zone{

if (!load) {

load = [super allocWithZone:zone];

}

return load;

}

//创建表

-(void)createTable{

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *dbPath = [path stringByAppendingString:@"/wx.db"];

_db = [[FMDatabase alloc] initWithPath:dbPath];

if ([_db open]) {

NSString *createTable = [NSString stringWithFormat:@"create table student(stuID integer primary key,stuName text,stuNumber text,stuTheory text,stuComputer text,stuRemarks text)"];

if ([_db executeUpdate:createTable]) {

NSLog(@"创建表成功!");

[_db close];

}

}

}

//添加数据

-(void)addData:(Student *)stu{

if ([_db open]) {

NSString *addData = [NSString stringWithFormat:@"insert into student(stuName,stuNumber,stuTheory,stuComputer,stuRemarks)values(%@,%@,%@,%@,%@)",stu.stuName,stu.stuNumber,stu.stuTheory,stu.stuComputer,stu.stuRemarks];

if ([_db executeUpdate:addData]) {

NSLog(@"数据添加成功");

[_db close];

}

}

}

//修改数据

-(void)updateData:(Student *)stu{

if ([_db open]) {

NSString *updateData = [NSString stringWithFormat:@"update student set stuName = %@,stuNumber = %@,stuTheory = %@,stuComputer = %@,stuRemarks = %@ where stuID = %ld",stu.stuName,stu.stuNumber,stu.stuTheory,stu.stuComputer,stu.stuRemarks,stu.stuID];

if ([_db executeUpdate:updateData]) {

NSLog(@"数据修改成功");

[_db close];

}

}

}

//删除数据

-(void)deleteData:(NSInteger)idl{

if ([_db open]) {

NSString *deleteData = [NSString stringWithFormat:@"delete from student where stuID = %ld",idl];

if ([_db executeUpdate:deleteData]) {

NSLog(@"数据删除成功");

[_db close];

}

}

}

//查询数据

-(NSMutableArray*)showAllData{

NSMutableArray *arr = [NSMutableArray array];

if ([_db open]) {

NSString *selectAllData = [NSString stringWithFormat:@"select * from student"];

FMResultSet *re = [_db executeQuery:selectAllData];

while ([re next]) {

Student *stu = [[Student alloc] init];

stu.stuID = [re intForColumn:@"stuID"];

stu.stuName = [re stringForColumn:@"stuName"];

stu.stuNumber = [re stringForColumn:@"stuNumber"];

stu.stuTheory = [re stringForColumn:@"stuTheory"];

stu.stuComputer = [re stringForColumn:@"stuComputer"];

stu.stuRemarks = [re stringForColumn:@"stuRemarks"];

[arr addObject:stu];

}

[_db close];

}

return arr;

}

//查询第一个数据

-(NSMutableArray *)showTop1Data{

NSMutableArray *arr = [NSMutableArray array];

if ([_db open]) {

NSString *selectAllData = [NSString stringWithFormat:@"select * from student where stuID = 1"];

FMResultSet *re = [_db executeQuery:selectAllData];

NSLog(@"%@",re);

while ([re next]) {

Student *stu = [[Student alloc] init];

stu.stuID = [re intForColumn:@"stuID"];

stu.stuName = [re stringForColumn:@"stuName"];

stu.stuNumber = [re stringForColumn:@"stuNumber"];

stu.stuTheory = [re stringForColumn:@"stuTheory"];

stu.stuComputer = [re stringForColumn:@"stuComputer"];

stu.stuRemarks = [re stringForColumn:@"stuRemarks"];

[arr addObject:stu];

}

[_db close];

NSLog(@"查询第一个数据成功");

}

return arr;

}

//查询理论分数最高分

-(NSMutableArray *)showTheoryMaxData{

NSMutableArray *arr = [NSMutableArray array];

if ([_db open]) {

NSString *selectAllData = [NSString stringWithFormat:@"select *  from student where stuTheory in (select max(stuTheory) from student)"];

FMResultSet *re = [_db executeQuery:selectAllData];

while ([re next]) {

Student *stu = [[Student alloc] init];

stu.stuID = [re intForColumn:@"stuID"];

stu.stuName = [re stringForColumn:@"stuName"];

stu.stuNumber = [re stringForColumn:@"stuNumber"];

stu.stuTheory = [re stringForColumn:@"stuTheory"];

stu.stuComputer = [re stringForColumn:@"stuComputer"];

stu.stuRemarks = [re stringForColumn:@"stuRemarks"];

[arr addObject:stu];

}

[_db close];

NSLog(@"查询理论分数最高分成功");

}

return arr;

}

//查询技能分数最高分

-(NSMutableArray *)showComputerMaxData{

NSMutableArray *arr = [NSMutableArray array];

if ([_db open]) {

NSString *selectAllData = [NSString stringWithFormat:@"select *  from student where stuComputer in (select max(stuComputer) from student)"];

FMResultSet *re = [_db executeQuery:selectAllData];

while ([re next]) {

Student *stu = [[Student alloc] init];

stu.stuID = [re intForColumn:@"stuID"];

stu.stuName = [re stringForColumn:@"stuName"];

stu.stuNumber = [re stringForColumn:@"stuNumber"];

stu.stuTheory = [re stringForColumn:@"stuTheory"];

stu.stuComputer = [re stringForColumn:@"stuComputer"];

stu.stuRemarks = [re stringForColumn:@"stuRemarks"];

[arr addObject:stu];

}

NSLog(@"查询技能分数最高分成功");

[_db close];

}

return arr;

}

//删除理论分数最低分

-(NSMutableArray *)deleteTheoryMinData{

NSMutableArray *arr = [NSMutableArray array];

if ([_db open]) {

NSString *selectAllData = [NSString stringWithFormat:@"select * from student where stuTheory in (select min(stuTheory) from student)"];

FMResultSet *re = [_db executeQuery:selectAllData];

Student *stu = [[Student alloc] init];

while ([re next]) {

stu.stuID = [re intForColumn:@"stuID"];

stu.stuName = [re stringForColumn:@"stuName"];

stu.stuNumber = [re stringForColumn:@"stuNumber"];

stu.stuTheory = [re stringForColumn:@"stuTheory"];

stu.stuComputer = [re stringForColumn:@"stuComputer"];

stu.stuRemarks = [re stringForColumn:@"stuRemarks"];

}

NSString *deleteTheoryMinData = [NSString stringWithFormat:@"delete from student where stuID = %ld",stu.stuID];

if ([_db executeUpdate:deleteTheoryMinData]) {

NSLog(@"删除理论成绩分数最低");

}

NSString *select1AllData = [NSString stringWithFormat:@"select * from student"];

FMResultSet *re1 = [_db executeQuery:select1AllData];

while ([re1 next]) {

Student *stu1 = [[Student alloc] init];

stu1.stuID = [re1 intForColumn:@"stuID"];

stu1.stuName = [re1 stringForColumn:@"stuName"];

stu1.stuNumber = [re1 stringForColumn:@"stuNumber"];

stu1.stuTheory = [re1 stringForColumn:@"stuTheory"];

stu1.stuComputer = [re1 stringForColumn:@"stuComputer"];

stu1.stuRemarks = [re1 stringForColumn:@"stuRemarks"];

[arr addObject:stu1];

}

[_db close];

}

return arr;

}

@end

-————————————————————————————

接下来用导航栏实现跳转

在APPDelegate里

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

MyTableViewController *vc = [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

self.window.rootViewController = nav;

return YES;

}

————————————————————————————

然后在现实界面写

Vi e w C o n t ro l l.m里写:

#import "MyTableViewController.h"

#import "AddViewController.h"

#import "LoadData.h"

@interface MyTableViewController ()

{

NSMutableArray *arr;

}

@end

@implementation MyTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

[[LoadData shareLoadData] createTable];

self.title = @"学生信心";

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加数据" style:UIBarButtonItemStylePlain target:self action:@selector(addClicked)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查询数据" style:UIBarButtonItemStylePlain target:self action:@selector(showAllData)];

}

-(void)viewDidAppear:(BOOL)animated{

arr =[[LoadData shareLoadData] showAllData];

[self.tableView reloadData];

}

-(void)addClicked{

AddViewController *vc = [[AddViewController alloc] init];

vc.idl = 0;

[self.navigationController pushViewController:vc animated:YES];

}

#if 0

-(void)showAllData{

//查询第一个数据

arr = [[LoadData shareLoadData] showTop1Data];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//查询理论成绩最高的学生

arr = [[LoadData shareLoadData] showTheoryMaxData];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//查询机试成绩最高的学生

arr = [[LoadData shareLoadData] showComputerMaxData];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//删除理论成绩最低分的纪录

arr = [[LoadData shareLoadData] deleteTheoryMinData];

[self.tableView reloadData];

}

#endif

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

return arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *str = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];

}

cell.textLabel.text = [arr[indexPath.row] stuName];

cell.detailTextLabel.text = [arr[indexPath.row] stuNumber];

return cell;

}

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the specified item to be editable.

return YES;

}

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

[[LoadData shareLoadData] deleteData:[arr[indexPath.row] stuID]];

[arr removeObjectAtIndex:indexPath.row];

[self.tableView reloadData];

}

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

AddViewController *vc = [[AddViewController alloc] init];

vc.idl = 1;

vc.st = arr[indexPath.row];

[self.navigationController pushViewController:vc animated:YES];

}

————————————————————————————

接下来在AddViewControl .h里


#import#import "Student.h"

#import "LoadData.h"

@interface AddViewController : UIViewController

@property (nonatomic, assign) NSInteger idl;

@property (nonatomic, strong) Student *st;

@end


————————————————————————————

.m里写


#import "AddViewController.h"

@interface AddViewController ()

@property (weak, nonatomic) IBOutlet UITextField *stuNameText;

@property (weak, nonatomic) IBOutlet UITextField *stuNumberText;

@property (weak, nonatomic) IBOutlet UITextField *stuTheoryText;

@property (weak, nonatomic) IBOutlet UITextField *stuComputerText;

@property (weak, nonatomic) IBOutlet UITextField *stuRemarksText;

@property (weak, nonatomic) IBOutlet UIButton *addButton;

@end

@implementation AddViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

if (self.idl == 0) {

[_addButton setTitle:@"添加数据" forState:UIControlStateNormal];

}else{

self.stuNameText.text = self.st.stuName;

self.stuNumberText.text = self.st.stuNumber;

self.stuTheoryText.text = self.st.stuTheory;

self.stuComputerText.text = self.st.stuComputer;

self.stuRemarksText.text = self.st.stuRemarks;

[_addButton setTitle:@"修改数据" forState:UIControlStateNormal];

}

}

- (IBAction)AddData:(id)sender {

if (self.idl == 0) {

Student *stu = [[Student alloc] init];

stu.stuName = self.stuNameText.text;

stu.stuNumber = self.stuNumberText.text;

stu.stuTheory = self.stuTheoryText.text;

stu.stuComputer = self.stuComputerText.text;

stu.stuRemarks = self.stuRemarksText.text;

[[LoadData shareLoadData] addData:stu];

[self.navigationController popViewControllerAnimated:YES];

}

else{

Student *stu = [[Student alloc] init];

stu.stuID = self.st.stuID;

stu.stuName = self.stuNameText.text;

stu.stuNumber = self.stuNumberText.text;

stu.stuTheory = self.stuTheoryText.text;

stu.stuComputer = self.stuComputerText.text;

stu.stuRemarks = self.stuRemarksText.text;

[[LoadData shareLoadData] updateData:stu];

[self.navigationController popViewControllerAnimated:YES];

}

}

FMDB增删改查(成绩查询)_第5张图片

xib如上

紧接着就完成了。

你可能感兴趣的:(FMDB增删改查(成绩查询))