#import "MainViewController.h"
#import "StudentManager.h"
#import <sqlite3.h>
#import "Student.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)dealloc
{
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//打开关闭
self.navigationController.navigationBar.translucent = NO;
UIButton *open = [UIButton buttonWithType:UIButtonTypeCustom];
[open setFrame:CGRectMake(60, 50, 80, 50)];
open.backgroundColor = [UIColor orangeColor];
[open setTitle:@"open" forState:UIControlStateNormal];
[open setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
open.layer.borderColor = [UIColor orangeColor].CGColor;
open.layer.borderWidth = 1.5f;
open.layer.cornerRadius = 20;
[self.view addSubview:open];
[open addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
[close setFrame:CGRectMake(180, 50, 80, 50)];
close.backgroundColor = [UIColor orangeColor];
[close setTitle:@"close" forState:UIControlStateNormal];
[close setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
close.layer.borderColor = [UIColor orangeColor].CGColor;
close.layer.borderWidth = 1.5f;
close.layer.cornerRadius = 20;
[self.view addSubview:close];
[close addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom];
[add setFrame:CGRectMake(60, 120, 80, 50)];
add.backgroundColor = [UIColor orangeColor];
[add setTitle:@"select" forState:UIControlStateNormal];
[add setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
add.layer.borderColor = [UIColor orangeColor].CGColor;
add.layer.borderWidth = 1.5f;
add.layer.cornerRadius = 20;
[self.view addSubview:add];
[add addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *delete = [UIButton buttonWithType:UIButtonTypeCustom];
[delete setFrame:CGRectMake(180, 120, 80, 50)];
delete.backgroundColor = [UIColor orangeColor];
[delete setTitle:@"delete" forState:UIControlStateNormal];
[delete setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
delete.layer.borderColor = [UIColor orangeColor].CGColor;
delete.layer.borderWidth = 1.5f;
delete.layer.cornerRadius = 20;
[self.view addSubview:delete];
[delete addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *insert = [UIButton buttonWithType:UIButtonTypeCustom];
[insert setFrame:CGRectMake(60, 190, 80, 50)];
insert.backgroundColor = [UIColor orangeColor];
[insert setTitle:@"insert" forState:UIControlStateNormal];
[insert setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
insert.layer.borderColor = [UIColor orangeColor].CGColor;
insert.layer.borderWidth = 1.5f;
insert.layer.cornerRadius = 20;
[self.view addSubview:insert];
[insert addTarget:self action:@selector(insertAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *update = [UIButton buttonWithType:UIButtonTypeCustom];
[update setFrame:CGRectMake(180, 190, 80, 50)];
update.backgroundColor = [UIColor orangeColor];
[update setTitle:@"update" forState:UIControlStateNormal];
[update setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
update.layer.borderColor = [UIColor orangeColor].CGColor;
update.layer.borderWidth = 1.5f;
update.layer.cornerRadius = 20;
[self.view addSubview:update];
[update addTarget:self action:@selector(updateAction:) forControlEvents:UIControlEventTouchUpInside];
// StudentManager *d = [StudentManager shareManager];
// [d open];
//增删改查
// NSString *create = @"CREATE TABLE IF NOT EXISTS STUDENT(stuid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT";
}
- (void)openAction:(id)sender
{
StudentManager *d = [StudentManager shareManager];
Student *stu = [[Student alloc] init];
stu.stuid = 2;
stu.name = @"xiaoqi";
stu.stuscore = 98.0;
[d openWithTable:@"yy"];
NSLog(@"打开%@",d);
}
- (void)closeAction:(id)sender
{
StudentManager *close = [StudentManager shareManager];
[close close];
NSLog(@"关闭%@",close);
}
- (void)insertAction:(id)sender
{
StudentManager *create = [StudentManager shareManager];
Student *stu = [[Student alloc] init];
stu.stuid = 1;
stu.name = @"xiaoqi";
stu.stuscore = 98.0;
NSLog(@"打开%@",create);
[create insertWithModel:stu];
[stu release];
}
- (void)deleteAction:(id)sender
{
StudentManager *q = [StudentManager shareManager];
[q deleteWithModel:1];
}
- (void)updateAction:(id)sender
{
StudentManager *q = [StudentManager shareManager];
Student *stu = [[Student alloc] init];
stu.name = @"xiaoqi";
stu.stuscore = 99.0;
stu.stuid = 1;
[q updateWithModel:stu];
[stu release];
}
- (void)selectAction:(id)sender
{
StudentManager *q = [StudentManager shareManager];
NSArray *arr = [q selectStuWithScore:90];
NSLog(@"%@",arr);
}
#import <Foundation/Foundation.h>
@class Student;
@interface StudentManager : NSObject
- (void)openWithTable:(NSString *)nameTable;
- (void)close;
- (void)createTable:(NSString *)tableName;
- (void)insertWithModel:(Student *)model;
- (void)deleteWithModel:(NSInteger)stuid;
- (NSArray *)selectStuWithScore:(CGFloat)score;
- (void)updateWithModel:(Student *)model;//不要忘记声明啊!
@property (nonatomic,retain) NSString *tableName;
+ (StudentManager *)shareManager;
@end
#import "StudentManager.h"
#import <sqlite3.h>
#import "Student.h"
//全局变量,默认为空
static StudentManager *manager = nil;
//定义数据库指针对象
static sqlite3 * dbPoint = nil;
@implementation StudentManager
- (void)dealloc
{
[_tableName release];
_tableName = nil;
[super dealloc];
}
//创建一个单例
+ (StudentManager *)shareManager
{
// //一般不用这个方法 无法保证唯一性
// if (!manager) {
// manager = [[StudentManager alloc] init];
//
// }
// return manager;
//声明一个GCD全局变量 保证初始化方法只走一次
static dispatch_once_t onceToken; //线程或单例安全 相当于加上