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