FMDB增刪改查(成績查詢)

首先

導(dǎo)入FMDB

并添加FMDB依賴庫(labslite3.0)

創(chuàng)建model類 ? ?如圖

緊接著創(chuàng)建業(yè)務(wù)處理層

如圖

代碼如下

+(instancetype)shareLoadData;

//創(chuàng)建表

-(void)createTable;

//添加數(shù)據(jù)

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

//修改數(shù)據(jù)

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

//刪除數(shù)據(jù)

-(void)deleteData:(NSInteger)idl;

//查詢數(shù)據(jù)

-(NSMutableArray*)showAllData;

//查詢第一個數(shù)據(jù)

-(NSMutableArray *)showTop1Data;

//查詢理論分?jǐn)?shù)最高分

-(NSMutableArray *)showTheoryMaxData;

//查詢技能分?jǐn)?shù)最高分

-(NSMutableArray *)showComputerMaxData;

//刪除理論分?jǐn)?shù)最低分

-(NSMutableArray *)deleteTheoryMinData;

接下來

#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;

}

//創(chuàng)建表

-(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(@"創(chuàng)建表成功!");

[_db close];

}

}

}

//添加數(shù)據(jù)

-(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(@"數(shù)據(jù)添加成功");

[_db close];

}

}

}

//修改數(shù)據(jù)

-(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(@"數(shù)據(jù)修改成功");

[_db close];

}

}

}

//刪除數(shù)據(jù)

-(void)deleteData:(NSInteger)idl{

if ([_db open]) {

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

if ([_db executeUpdate:deleteData]) {

NSLog(@"數(shù)據(jù)刪除成功");

[_db close];

}

}

}

//查詢數(shù)據(jù)

-(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;

}

//查詢第一個數(shù)據(jù)

-(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(@"查詢第一個數(shù)據(jù)成功");

}

return arr;

}

//查詢理論分?jǐn)?shù)最高分

-(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(@"查詢理論分?jǐn)?shù)最高分成功");

}

return arr;

}

//查詢技能分?jǐn)?shù)最高分

-(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(@"查詢技能分?jǐn)?shù)最高分成功");

[_db close];

}

return arr;

}

//刪除理論分?jǐn)?shù)最低分

-(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(@"刪除理論成績分?jǐn)?shù)最低");

}

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

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

接下來用導(dǎo)航欄實現(xiàn)跳轉(zhuǎn)

在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;

}

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

然后在現(xiàn)實界面寫

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 = @"學(xué)生信心";

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加數(shù)據(jù)" style:UIBarButtonItemStylePlain target:self action:@selector(addClicked)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查詢數(shù)據(jù)" 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{

//查詢第一個數(shù)據(jù)

arr = [[LoadData shareLoadData] showTop1Data];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//查詢理論成績最高的學(xué)生

arr = [[LoadData shareLoadData] showTheoryMaxData];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//查詢機試成績最高的學(xué)生

arr = [[LoadData shareLoadData] showComputerMaxData];

[self.tableView reloadData];

}

#elif 0

-(void)showAllData{

//刪除理論成績最低分的紀(jì)錄

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:@"添加數(shù)據(jù)" 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:@"修改數(shù)據(jù)" 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];

}

}

xib如上

緊接著就完成了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容