数据库保存图片,bool的例子,其实不应该保存图片,保存图影响速度,应该保存图片的路径,除非特殊需求。
#import "DataBase.h"
#import "ProjectModel.h"
static DataBase *_DBCtl = nil;
@interface DataBase(){
FMDatabase *_db;
}
@end
@implementation DataBase
+(instancetype)sharedDataBase{
if (_DBCtl == nil) {
_DBCtl = [[DataBase alloc] init];
[_DBCtl initDataBase];
}
return _DBCtl;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (_DBCtl == nil) {
_DBCtl = [super allocWithZone:zone];
}
return _DBCtl;
}
-(id)copy{
return self;
}
-(id)mutableCopy{
return self;
}
-(id)copyWithZone:(NSZone *)zone{
return self;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
return self;
}
-(void)initDataBase{
// 获得Documents目录路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 文件路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"model.sqlite"];
// 实例化FMDataBase对象
_db = [FMDatabase databaseWithPath:filePath];
[_db open];
// 初始化数据表
NSString *Sql = @"CREATE TABLE 'project' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,'model_id' VARCHAR(255),'name' TEXT,'startImg' BLOB,'screenShot' BLOB,'endImg' BLOB,'createTime' TEXT,'endTime' TEXT,'infos' BLOB,'imgIsUpload' INTEGER,'ponitsIsUpload' INTEGER) ";
[_db executeUpdate:Sql];
[_db close];
}
#pragma mark - 接口
- (void)addProjectModel:(ProjectModel *)projectModel{
[_db open];
NSNumber *maxID = @(0);
FMResultSet *res = [_db executeQuery:@"SELECT * FROM project "];
//获取数据库中最大的ID
while ([res next]) {
if ([maxID integerValue] < [[res stringForColumn:@"model_id"] integerValue]) {
maxID = @([[res stringForColumn:@"model_id"] integerValue] ) ;
}
}
maxID = @([maxID integerValue] + 1);
[_db executeUpdate:@"INSERT INTO project(model_id,name,startImg,screenShot,endImg,createTime,endTime,infos,imgIsUpload,ponitsIsUpload)VALUES(?,?,?,?,?,?,?,?,?,?)",maxID,projectModel.name, [NSKeyedArchiver archivedDataWithRootObject:projectModel.startImg], [NSKeyedArchiver archivedDataWithRootObject:projectModel.screenShot], [NSKeyedArchiver archivedDataWithRootObject:projectModel.endImg], projectModel.createTime, projectModel.endTime, [NSKeyedArchiver archivedDataWithRootObject:projectModel.infos], @(projectModel.imgIsUpload), @(projectModel.ponitsIsUpload)];
[_db close];
}
- (void)deleteProjectModel:(ProjectModel *)projectModel{
[_db open];
[_db executeUpdate:@"DELETE FROM project WHERE model_id = ?",projectModel.Id];
[_db close];
}
- (void)updateProjectModel:(ProjectModel *)projectModel{
[_db open];
[_db executeUpdate:@"UPDATE 'project' SET name = ? WHERE model_id = ? ",projectModel.name,projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET startImg = ? WHERE model_id = ? ",[NSKeyedArchiver archivedDataWithRootObject:projectModel.startImg],projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET screenShot = ? WHERE model_id = ? ",[NSKeyedArchiver archivedDataWithRootObject:projectModel.screenShot],projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET endImg = ? WHERE model_id = ? ",[NSKeyedArchiver archivedDataWithRootObject:projectModel.endImg],projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET createTime = ? WHERE model_id = ? ",projectModel.createTime,projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET endTime = ? WHERE model_id = ? ",projectModel.endTime,projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET infos = ? WHERE model_id = ? ",[NSKeyedArchiver archivedDataWithRootObject:projectModel.infos],projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET imgIsUpload = ? WHERE model_id = ? ",@(projectModel.imgIsUpload),projectModel.Id];
[_db executeUpdate:@"UPDATE 'project' SET ponitsIsUpload = ? WHERE model_id = ? ",@(projectModel.ponitsIsUpload),projectModel.Id];
[_db close];
}
- (NSMutableArray *)getAllProjectModel{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
//ORDER BY %@ DESC LIMIT 10
FMResultSet *res = [_db executeQuery:@"SELECT * FROM project ORDER BY createTime DESC LIMIT 10"];
while ([res next]) {
ProjectModel *projectModel = [[ProjectModel alloc] init];
projectModel.Id = @([[res stringForColumn:@"model_id"] integerValue]);
projectModel.name = [res stringForColumn:@"name"];
projectModel.startImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"startImg"]];
projectModel.screenShot = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"screenShot"]];
projectModel.endImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"endImg"]];
projectModel.createTime = [res stringForColumn:@"createTime"];
projectModel.endTime = [res stringForColumn:@"endTime"];
projectModel.infos = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"infos"]];
projectModel.imgIsUpload = [res boolForColumn:@"imgIsUpload"];
projectModel.ponitsIsUpload = [res boolForColumn:@"ponitsIsUpload"];
[dataArray addObject:projectModel];
}
[_db close];
return dataArray;
}
- (NSMutableArray *)getFirstProjectModel{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
//ORDER BY %@ DESC LIMIT 10
FMResultSet *res = [_db executeQuery:@"SELECT * FROM project ORDER BY createTime DESC LIMIT 1"];
while ([res next]) {
ProjectModel *projectModel = [[ProjectModel alloc] init];
projectModel.Id = @([[res stringForColumn:@"model_id"] integerValue]);
projectModel.name = [res stringForColumn:@"name"];
projectModel.startImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"startImg"]];
projectModel.screenShot = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"screenShot"]];
projectModel.endImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"endImg"]];
projectModel.createTime = [res stringForColumn:@"createTime"];
projectModel.endTime = [res stringForColumn:@"endTime"];
projectModel.infos = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"infos"]];
projectModel.imgIsUpload = [res boolForColumn:@"imgIsUpload"];
projectModel.ponitsIsUpload = [res boolForColumn:@"ponitsIsUpload"];
[dataArray addObject:projectModel];
}
[_db close];
return dataArray;
}
- (NSMutableArray *)getPartProjectModel{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
//ORDER BY %@ DESC LIMIT 10
FMResultSet *res = [_db executeQuery:@"SELECT model_id,name,createTime,imgIsUpload,ponitsIsUpload FROM project ORDER BY createTime DESC LIMIT 10"];
while ([res next]) {
ProjectModel *projectModel = [[ProjectModel alloc] init];
projectModel.Id = @([[res stringForColumn:@"model_id"] integerValue]);
projectModel.name = [res stringForColumn:@"name"];
// projectModel.startImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"startImg"]];
// projectModel.screenShot = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"screenShot"]];
// projectModel.endImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"endImg"]];
projectModel.createTime = [res stringForColumn:@"createTime"];
// projectModel.endTime = [res stringForColumn:@"endTime"];
// projectModel.infos = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"infos"]];
projectModel.imgIsUpload = [res boolForColumn:@"imgIsUpload"];
projectModel.ponitsIsUpload = [res boolForColumn:@"ponitsIsUpload"];
[dataArray addObject:projectModel];
}
[_db close];
return dataArray;
}
- (NSMutableArray *)getImgProjectModelByName:(NSString *)name{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
//ORDER BY %@ DESC LIMIT 10
NSString *sql = [NSString stringWithFormat:@"SELECT model_id, startImg,screenShot,endImg FROM project WHERE name='%@'",name];
FMResultSet *res = [_db executeQuery:sql];
while ([res next]) {
ProjectModel *projectModel = [[ProjectModel alloc] init];
projectModel.Id = @([[res stringForColumn:@"model_id"] integerValue]);
// projectModel.name = [res stringForColumn:@"name"];
projectModel.startImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"startImg"]];
projectModel.screenShot = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"screenShot"]];
projectModel.endImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"endImg"]];
// projectModel.createTime = [res stringForColumn:@"createTime"];
// projectModel.endTime = [res stringForColumn:@"endTime"];
// projectModel.infos = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"infos"]];
// projectModel.imgIsUpload = [res boolForColumn:@"imgIsUpload"];
// projectModel.ponitsIsUpload = [res boolForColumn:@"ponitsIsUpload"];
[dataArray addObject:projectModel];
}
[_db close];
return dataArray;
}
- (NSMutableArray *)getInfosProjectModelByName:(NSString *)name{
[_db open];
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSString *sql = [NSString stringWithFormat:@"SELECT model_id,name,createTime,endTime,infos,imgIsUpload,ponitsIsUpload FROM project WHERE name='%@'",name];
FMResultSet *res = [_db executeQuery:sql];
while ([res next]) {
ProjectModel *projectModel = [[ProjectModel alloc] init];
projectModel.Id = @([[res stringForColumn:@"model_id"] integerValue]);
projectModel.name = [res stringForColumn:@"name"];
// projectModel.startImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"startImg"]];
// projectModel.screenShot = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"screenShot"]];
// projectModel.endImg = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"endImg"]];
projectModel.createTime = [res stringForColumn:@"createTime"];
projectModel.endTime = [res stringForColumn:@"endTime"];
projectModel.infos = [NSKeyedUnarchiver unarchiveObjectWithData:[res dataForColumn:@"infos"]];
projectModel.imgIsUpload = [res boolForColumn:@"imgIsUpload"];
projectModel.ponitsIsUpload = [res boolForColumn:@"ponitsIsUpload"];
[dataArray addObject:projectModel];
}
[_db close];
return dataArray;
}
@end