您的位置:首页 > 数据库

218,使用单例模型定义FMDB的操作,使得数据库操作跟ViewController分离

2016-01-18 21:27 423 查看
Student.h:

#import <Foundation/Foundation.h>

@interface Student :
NSObject

@property (nonatomic,copy)
NSString *name;

@property (nonatomic,assign)
int age;

- (instancetype)initWithName:(NSString *)name WithAge:(int)age;

+ (instancetype)studentWithName:(NSString *)name WithAge:(int)age;

@end

Student.m:

#import "Student.h"

@implementation Student

- (instancetype)initWithName:(NSString *)name WithAge:(int)age{

self = [super
init];

if (self) {

self.name = name;

self.age = age;

}

return
self;

}

+ (instancetype)studentWithName:(NSString *)name WithAge:(int)age{

return [[self
alloc] initWithName:name
WithAge:age];

}

@end

FMDBTool.h:

#import <Foundation/Foundation.h>

@class Student;

@interface FMDBTool :
NSObject

//获取单例

+ (instancetype)shareTool;

//插入学生数据

- (BOOL)insertStudent:(Student *)student;

//查询学生数据

- (NSMutableArray *)queryStudent;

@end

FMDBTool.m:

#import "FMDBTool.h"

#import "FMDatabase.h"

#import "Student.h"

@implementation FMDBTool

+ (instancetype)shareTool{

FMDBTool *instacne = [[self
alloc] init];

return instacne;

}

static FMDBTool *_instance =
nil;

static FMDatabase *_db;

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

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

//初始化单例

_instance = [[super
allocWithZone:zone]
init];

//1.获得数据库文件的路径

NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];

NSString *fileName=[doc
stringByAppendingPathComponent:@"student.sqlite"];

//2.获得数据库

_db =[FMDatabase
databaseWithPath:fileName];

//3.打开数据库

if ([_db
open]) {

//4.创表

BOOL result=[_db
executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);"];

if (result) {

NSLog(@"创表成功");

}else

{

NSLog(@"创表失败");

}

}

});

return
_instance;

}

- (BOOL)insertStudent:(Student *)student{

BOOL result = [_db
executeUpdateWithFormat:@"INSERT INTO t_student (name, age) VALUES (%@, %d);",student.name,student.age];

return result;

}

- (NSMutableArray *)queryStudent{

FMResultSet *set = [_db
executeQueryWithFormat:@"SELECT NAME,AGE FROM T_STUDENT"];

NSMutableArray *studentsM = [NSMutableArray
array];

while (set.next) {

Student *stu = [Student
studentWithName:[set stringForColumn:@"name"]
WithAge:[set intForColumn:@"age"]];

[studentsM addObject:stu];

}

return studentsM;

}

@end

ViewController.h:

#import "ViewController.h"

#import "FMDBTool.h"

#import "Student.h"

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

FMDBTool *tool = [FMDBTool
shareTool];

Student *stu = [Student
studentWithName:@"李四"
WithAge:16];

NSLog(@"%i",[tool
insertStudent:stu]);

NSMutableArray *students = [tool
queryStudent];

for (Student *stu
in students) {

NSLog(@"name = %@,age = %i",stu.name,stu.age);

}

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: