//
// TYDataBase.h
// wrxs523
//
// Created by wbb on 2023/5/29.
//
#import <Foundation/Foundation.h>
#import "TYUserInfo.h"
NS_ASSUME_NONNULL_BEGIN
@interface TYDataBase : NSObject
+(instancetype)shareInstance;
//创建照片标准表
-(void)createStandardsDatabase;
//增
-(BOOL)addPhotoModel:(TYUserInfo *)photoModel;
//删
-(void)deletePhotoModel:(TYUserInfo *)photoModel;
- (void)deleteAll;
- (void)deletetable;
//改
-(void)updatePhotoModel:(TYUserInfo *)photoModel;
//查
-(TYUserInfo *)getPhotoModel:(TYUserInfo *)photoModel;
-(TYUserInfo *)getPhotoModelWithEmail:(NSString *)email;
- (UIImage *)queryImageWithId:(NSString *)email;
@end
NS_ASSUME_NONNULL_END
//
// TYDataBase.m
// wrxs523
//
// Created by wbb on 2023/5/29.
//
#import "TYDataBase.h"
#import "FMDB.h"
@interface TYDataBase ()
@property(nonatomic, strong) NSString *dataBasePath;
@property(nonatomic, strong) FMDatabase *standardDatabase;
@end
@implementation TYDataBase
+(instancetype)shareInstance
{
static dispatch_once_t onceToken;
static TYDataBase * standardDatabase = nil;
dispatch_once(&onceToken, ^{
standardDatabase = [[TYDataBase alloc]init];
[standardDatabase createStandardsDatabase];
});
return standardDatabase;
}
//数据库地址
-(NSString *)dataBasePath{
if (_dataBasePath == nil) {
NSString *pathes = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// NSString * pathes = @"/Users/gonganxinxi/Desktop";
_dataBasePath = [pathes stringByAppendingPathComponent:@"my.sqlite"];
}
return _dataBasePath;
}
//创建照片标准表
-(void)createStandardsDatabase
{
self.standardDatabase = [FMDatabase databaseWithPath:self.dataBasePath];
if ([self.standardDatabase open]) {
NSLog(@"数据库打开成功");
NSString *sqString = @"create table if not exists standard (s_id integer primary key autoincrement not NULL, s_clcsEmail text,s_clcsPassword text,s_clcsNickname text,s_clcsGender text,s_clcsAge text, s_clcsData blob,s_clcxUserInfo blob)";
BOOL result = [self.standardDatabase executeUpdate:sqString];
if (result) {
NSLog(@"创建表成功");
}else{
NSLog(@"创建表失败");
}
}
}
//增
-(BOOL)addPhotoModel:(TYUserInfo *)photoModel
{
// TYUserInfo * model = photoModel;
// NSLog(@"数据库%ld, %@, %@, %@, %@, %@, %@", (long)model.ID, model.CSM, model.CSZ, model.CSSM, model.ZPLX,model.DQDM,model.SFYX);
if ([self isExistDataWithContent:photoModel.email]) {
[self updatePhotoModel:photoModel];
return NO;
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:photoModel];
BOOL result = [self.standardDatabase executeUpdateWithFormat:@"insert into standard (s_clcsEmail, s_clcsPassword, s_clcsNickname, s_clcsGender , s_clcsAge,s_clcsData, s_clcxUserInfo) values (%@, %@, %@, %@, %@,%@, %@)", photoModel.email, photoModel.password, photoModel.nickname, photoModel.gender, photoModel.age,photoModel.imageData,data];
if (result) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败");
}
return result;
}
//删
-(void)deletePhotoModel:(TYUserInfo *)photoModel
{
TYUserInfo * model = photoModel;
BOOL result = [self.standardDatabase executeUpdateWithFormat:@"delete from standard where s_clcsEmail = %@ and s_clcsPassword = %@ ", photoModel.email,photoModel.password];
if (result) {
NSLog(@"删除数据成功");
}else{
NSLog(@"删除数据失败");
}
}
- (void)deleteAll
{
BOOL result = [self.standardDatabase executeUpdateWithFormat:@"delete from standard"];
if (result) {
NSLog(@"删除全部数据成功");
}else{
NSLog(@"删除全部数据失败");
}
}
- (void)deletetable
{
BOOL result = [self.standardDatabase executeUpdateWithFormat:@"drop table if exists standard"];
if (result) {
NSLog(@"删除表成功");
}else{
NSLog(@"删除表失败");
}
}
//改
-(void)updatePhotoModel:(TYUserInfo *)photoModel
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:photoModel];
BOOL result = [self.standardDatabase executeUpdateWithFormat:@"update standard set s_clcsEmail = %@,s_clcsPassword = %@, s_clcsNickname = %@,s_clcsGender = %@,s_clcsAge = %@,s_clcsData = %@,s_clcxUserInfo = %@ where s_clcsEmail = %@", photoModel.email, photoModel.password, photoModel.nickname, photoModel.gender, photoModel.age,photoModel.imageData,data,photoModel.email];
if (result) {
NSLog(@"更改数据成功");
}else{
NSLog(@"更改数据失败");
}
}
//查 : 根据需要,查到响应的model返回,我们可以从中获取model的属性值
-(TYUserInfo *)getPhotoModel:(TYUserInfo *)photoModel
{
TYUserInfo * returnModel = nil;
FMResultSet *result = [self.standardDatabase executeQueryWithFormat:@"select s_clcxUserInfo from standard where s_clcsEmail = %@ ", photoModel.email];
while ([result next]) {
NSData *data = [result dataForColumn:@"s_clcxUserInfo"];
returnModel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
return returnModel;
}
-(TYUserInfo *)getPhotoModelWithEmail:(NSString *)email
{
TYUserInfo * returnModel = nil;
FMResultSet *result = [self.standardDatabase executeQueryWithFormat:@"select s_clcxUserInfo from standard where s_clcsEmail = %@ ", email];
while ([result next]) {
NSData *data = [result dataForColumn:@"s_clcxUserInfo"];
returnModel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
return returnModel;
}
// 判断数据库中是否已经存在该数据
- (BOOL)isExistDataWithContent:(NSString *)email {
FMResultSet *set = [self.standardDatabase executeQuery:@"SELECT * FROM standard WHERE s_clcsEmail = ?",email];
return [set next];
}
// 存储图片
- (BOOL)insertImage:(UIImage *)image withEmail:(NSString *)email {
NSData *imageData = UIImagePNGRepresentation(image);
BOOL success = [self.standardDatabase executeUpdate:@"INSERT INTO standard (s_clcsData) VALUES (?) where s_clcsEmail = ?", imageData,email];
return success;
}
// 查询图片
- (UIImage *)queryImageWithId:(NSString *)email {
FMResultSet *resultSet = [self.standardDatabase executeQuery:@"SELECT * FROM standard WHERE s_clcsEmail = ?", email];
UIImage *image = nil;
if ([resultSet next]) {
NSData *imageData = [resultSet dataForColumn:@"s_clcsData"];
image = [UIImage imageWithData:imageData];
}
return image;
}
@end
//
// TYUserInfo.h
// wrxs523
//
// Created by wbb on 2023/5/25.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TYUserInfo : NSObject
@property (nonatomic, copy) NSString * email;
@property (nonatomic, copy) NSString * password;
@property (nonatomic, copy) NSString * nickname;
@property (nonatomic, copy) NSString * gender;
@property (nonatomic, copy) NSString * age;
@property (nonatomic, strong) NSData * imageData;
@end
NS_ASSUME_NONNULL_END
#import "TYUserInfo.h"
#import "MJExtension.h"
@implementation TYUserInfo
//一句话将所有的子文件全部归档反归档(MJExtension)
MJCodingImplementation
@end
网友评论