FMDB增刪改查(成績查詢)

首先

導入FMDB

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

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

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

如圖

代碼如下

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

//查詢理論分數(shù)最高分

-(NSMutableArray *)showTheoryMaxData;

//查詢技能分數(shù)最高分

-(NSMutableArray *)showComputerMaxData;

//刪除理論分數(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;

}

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

}

return arr;

}

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

[_db close];

}

return arr;

}

//刪除理論分數(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(@"刪除理論成績分數(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

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

接下來用導航欄實現(xià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 = @"學生信心";

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{

//查詢理論成績最高的學生

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:@"添加數(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如上

緊接著就完成了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市窗怒,隨后出現(xiàn)的幾起案子允乐,更是在濱河造成了極大的恐慌猛遍,老刑警劉巖谷誓,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掸绞,死亡現(xiàn)場離奇詭異淹遵,居然都是意外死亡继找,警方通過查閱死者的電腦和手機强衡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來码荔,“玉大人漩勤,你說我怎么就攤上這事∷踅粒” “怎么了越败?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長硼瓣。 經(jīng)常有香客問我究飞,道長,這世上最難降的妖魔是什么堂鲤? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任亿傅,我火速辦了婚禮,結果婚禮上瘟栖,老公的妹妹穿的比我還像新娘葵擎。我一直安慰自己,他們只是感情好半哟,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布酬滤。 她就那樣靜靜地躺著,像睡著了一般寓涨。 火紅的嫁衣襯著肌膚如雪盯串。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天戒良,我揣著相機與錄音体捏,去河邊找鬼。 笑死糯崎,一個胖子當著我的面吹牛几缭,可吹牛的內容都是我干的。 我是一名探鬼主播拇颅,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼奏司,長吁一口氣:“原來是場噩夢啊……” “哼乔询!你這毒婦竟也來了樟插?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎黄锤,沒想到半個月后搪缨,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡鸵熟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年副编,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片流强。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡痹届,死狀恐怖,靈堂內的尸體忽然破棺而出打月,到底是詐尸還是另有隱情队腐,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布奏篙,位于F島的核電站柴淘,受9級特大地震影響,放射性物質發(fā)生泄漏秘通。R本人自食惡果不足惜为严,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望肺稀。 院中可真熱鬧第股,春花似錦、人聲如沸话原。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽稿静。三九已至梭冠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間改备,已是汗流浹背控漠。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留悬钳,地道東北人盐捷。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像默勾,于是被迫代替她去往敵國和親碉渡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內容

  • 用cocoaPods配置第三方文件 第一步母剥。打開終端 第二步滞诺。cd+文件夾 第三步形导。pod init 第四步。打開...
    不說謊的匹諾曹Y閱讀 1,089評論 0 1
  • 開始使用SQLite所需要的幾個步驟 1.需要導入的框架:libsqlite3.0.tbd 2.創(chuàng)建Model類L...
    井底蛙之呱呱閱讀 754評論 0 1
  • 最近項目中用到本地數(shù)據(jù)庫存儲數(shù)據(jù)习霹,將具體的實現(xiàn)記錄一下朵耕。 1.數(shù)據(jù)庫的創(chuàng)建,創(chuàng)建了一個單例文件淋叶。.h文件代碼如下阎曹。...
    妃雪閱讀 1,499評論 0 2
  • 哦吼吼,又研究了幾天煞檩,把FMDB這個封裝好的數(shù)據(jù)庫搞定了处嫌,寫了個簡單的例子,基于FMDB的添刪改查操作斟湃,界面很一般...
    lichengjin閱讀 531評論 0 0
  • 作者唯一QQ:228544117锰霜。。桐早。癣缅。。 =========后面的都要新建一個文章 AppDelegate.h ...
    CC_iOS閱讀 873評論 0 0