学生信息管理

#import <Foundation/Foundation.h>


@interface Student : NSObject

@property (nonatomic, strong) NSString *stuName,*stuNumber,*stuTheory,*stuComputer,*stuRemarks;

@property (nonatomic, assign) NSInteger stuID;

@end



#import <Foundation/Foundation.h>

#import <FMDB.h>

#import "Student.h"

#import "LoadData.h"

@interface LoadData : NSObject


+(instancetype)shareLoadData;


//创建表

-(void)createTable;


//添加数据

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


//修改数据

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


//删除数据

-(void)deleteData:(NSInteger)idl;


//查询数据

-(NSMutableArray*)showAllData;



//



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

}

//

#import "AppDelegate.h"

#import "MyTableViewController.h"

@interface AppDelegate ()


@end


@implementation 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;

}



//


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

}


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

}




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

}



//


#import <UIKit/UIKit.h>

#import "Student.h"

#import "LoadData.h"

@interface AddViewController : UIViewController

@property (nonatomic, assign) NSInteger idl;

@property (nonatomic, strong) Student *st;

@end



//



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

    }

}


pod 'FMDB', '~> 2.5'






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值