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
#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
相关文章推荐
- MariaDB命令详解
- Mysql的函数使用方法
- 使用SQL语句 进行数据持久化
- sql server存储过程
- Redis使用总结
- iOS FMDB数据库加密相关
- 数据库不能open下查看undo段的名字
- Oracle定时计划快速使用
- Oracle定时计划快速使用
- Postgresql 正则表达式(转)
- Oracle定时计划快速使用
- MongoDB 初见指南
- MongoDB 初见指南
- MongoDB 初见指南
- MongoDB 初见指南
- ios开发FMDB导入SQLCipher加密数据库
- MSSQL 2012安装报错之0x858C001B
- MySQL学习8_mysqld与mysql命令区别
- (转)数据库表分割技术浅析(水平分割/垂直分割/库表散列)
- wamp --mysql 控制台总是在Enterpasswd后闪退