FMDB转载


//Student -objc

@interface Student : NSObject


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

@property (nonatomic, assign) NSInteger stud;


// Mydata-h

#import "Student.h"


@interface MyData : NSObject


+ (instancetype)shareLoadData;


//添加数据

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


//查询数据

-(NSMutableArray*)showAllData;


//查询第一个数据

- (NSMutableArray *)showTop1Data;




// Mydata-m

#import "FMDatabase.h"


static FMDatabase *db;

static MyData *data = nil;


@implementation MyData


+(instancetype)shareLoadData{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        data = [[MyData alloc] init];

        [data createTable];

        

    });

    return data;

}


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

    if (!data) {

        data = [super allocWithZone:zone];

    }

    return data;

}


//创建表

-(void)createTable{

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

    NSString *dbPath = [path stringByAppendingPathComponent:@"wwxww.db"];

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

    if ([db open]) {

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

        if ([db executeUpdate:createTable]) {

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

            [db close];

        }

        

    }

    

}


//添加数据

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

    [db open];

    BOOL isb = [db executeUpdate:@"insert into student values(null,?,?,?,?,?)",stu.stuName,stu.stuNumber,stu.stuTheory,stu.stuComputer,stu.stuRemarks];

    

    if (isb) {

        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;

}



// view-m

#import "MyData.h"

#import "AddViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSArray *arr;

    UITableView *table;

}


- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    arr = [[MyData shareLoadData] showAllData];

}


//添加按钮

    UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    add.frame = CGRectMake(0, 80, self.view.frame.size.width/4, 40);

    [add setTitle:@"添加" forState:UIControlStateNormal];

    [add addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:add];

//  添加

-(void)click1{

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

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

     

}

//查询

-(void)click4{

    //查询第一个数据

    

    arr = [[MyData shareLoadData] showTop1Data];

    [table reloadData];

    

}

#pragma mark - Table view data source


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

    return 1;

}


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

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

    Student *stu = arr[indexPath.row];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@--%@--%@--%@--",stu.stuNumber,stu.stuTheory,stu.stuComputer,stu.stuRemarks];

    return cell;

}


//add-h

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存数据" style:UIBarButtonItemStylePlain target:self action:@selector(showData)];

        }

- (void)showData

{

    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;

    [[MyData shareLoadData] addData:stu];

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值