FMDB转载

来源:互联网 发布:php电商项目架构 编辑:程序博客网 时间:2024/06/05 19:46


//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 = [[MyDataalloc] init];

        [data createTable];

        

    });

    return data;

}


+(instancetype)allocWithZone:(struct_NSZone *)zone{

    if (!data) {

        data = [superallocWithZone:zone];

    }

    return data;

}


//创建表

-(void)createTable{

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

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

    db = [[FMDatabasealloc] initWithPath:dbPath];

    if ([dbopen]) {

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

        if ([dbexecuteUpdate:createTable]) {

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

            [db close];

        }

        

    }

    

}


//添加数据

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

    [db open];

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

    

    if (isb) {

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

        [db close];

    }

}

//查询数据

- (NSMutableArray*)showAllData{

    NSMutableArray *arr = [NSMutableArrayarray];

    if ([dbopen]) {

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

        FMResultSet *re = [dbexecuteQuery:selectAllData];

        while ([re next]) {

            Student *stu = [[Studentalloc] init];

            stu.stuID = [reintForColumn:@"stuID"];

            stu.stuName = [restringForColumn:@"stuName"];

            stu.stuNumber = [restringForColumn:@"stuNumber"];

            stu.stuTheory = [restringForColumn:@"stuTheory"];

            stu.stuComputer = [restringForColumn:@"stuComputer"];

            stu.stuRemarks = [restringForColumn:@"stuRemarks"];

            [arr addObject:stu];

        }

        [db close];

    }

    return arr;

}

//查询第一个数据

-(NSMutableArray *)showTop1Data{

    NSMutableArray *arr = [NSMutableArrayarray];

    if ([dbopen]) {

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

        FMResultSet *re = [dbexecuteQuery:selectAllData];

        NSLog(@"%@",re);

        while ([re next]) {

            Student *stu = [[Studentalloc] init];

            stu.stuID = [reintForColumn:@"stuID"];

            stu.stuName = [restringForColumn:@"stuName"];

            stu.stuNumber = [restringForColumn:@"stuNumber"];

            stu.stuTheory = [restringForColumn:@"stuTheory"];

            stu.stuComputer = [restringForColumn:@"stuComputer"];

            stu.stuRemarks = [restringForColumn:@"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 = [[MyDatashareLoadData] showAllData];

}


//添加按钮

    UIButton *add = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

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

    [self.viewaddSubview:add];

//  添加

-(void)click1{

    AddViewController *vc = [[AddViewControlleralloc] init];

    [self.navigationControllerpushViewController:vc animated:YES];

     

}

//查询

-(void)click4{

    //查询第一个数据

    

    arr = [[MyDatashareLoadData] 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 = [tableViewdequeueReusableCellWithIdentifier:str];

    if (!cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:str];

    }

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

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

    Student *stu = arr[indexPath.row];

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

    return cell;

}


//add-h

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"保存数据"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(showData)];

        }

- (void)showData

{

    Student *stu = [[Studentalloc] 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];

}





0 0
原创粉丝点击