清除缓存的一个简单操作:
这里需要在导入三方库的条件下
1.获取缓存,如果是图片:
获取缓存
NSString *title = nil;
NSUInteger size = [SDImageCache sharedImageCache].getSize;
获取的size是byte
if(size > 1024*1024){
CGFloat floatSize = size/1024.0/1024.0;
title = [NSString stringWithFormat:@"%.1luM",(unsigned long)floatSize];
}else if(size > 1024){
CGFloat floatSize = size/1024.0;
title = [NSString stringWithFormat:@"%.1luKB",(unsigned long)floatSize];
}else {
title = [NSString stringWithFormat:@"%ludb",(unsigned long)size];
}
清空缓存
[[SDImageCache sharedImageCache]clearDisk];
数据库的操作:
1.系统的sqlite:
优点:
1> 存储大批量数据,可以精确的读取数据。
2> 批量读取数据,NSCoding这些都是一次把所有数据读取出来。
1.引入头文件
#import <sqlite3.h>
2.在Build Phases里的 Link Binary With Libraries里添加
libsqlite3.0.dylib或者libsqlite3.dylib
3.打开数据库,首先创建数据库,现在存储放在cache文件中
NSString *cachePath =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)[0];
拼接文件路径,存储在数据库的路径中
NSString *filePath = [cachePathstringByAppendingPathComponent:@"student.sqlite"];
4.给一个数据库实例 ppDb
sqlite3 *db;
5.判断打开与否
if (sqlite3_open(filePath.UTF8String, &_db) == SQLITE_OK) {
//打开成功
NSLog(@"success");
}else{
NSLog(@"default");
}
6.创建数据库表
//成功要创建
NSString *sql =@"create table if not exists t_contact (id integer primary key autoincrement,name varchar(255),age varchar(255));";
char *errmsg ;//传个地址。找到后赋值
sqlite3_exec(_db, sql.UTF8String,NULL,NULL, &errmsg);
if(errmsg){
NSLog(@"创建失败");
}else{
NSLog(@"创建成功");
}
7.插入数据
contact.name,contact.age是两个模型,_db是数据库实例
NSString *sql = [NSStringstringWithFormat:@"insert into t_contact (name,age) values('%@','%@');",contact.name,contact.age];
char *errmsg ;//传个地址。找到后赋值
sqlite3_exec(_db, sql.UTF8String,NULL,NULL, &errmsg);
if(errmsg){
flag =NO;
NSLog(@"操作失败");
}else{
flag =YES;
NSLog(@"操作成功");
}
8.查询数据
NSString *sql = @"select * from t_contact;"//准备查,生成一个巨屏
sqlite3_stmt *stmt;
NSMutableArray *detailArray = [NSMutableArrayarray];
if (sqlite3_prepare_v2(_db, sql.UTF8String, -1, &stmt ,NULL) ==SQLITE_OK) {
//执行巨屏
while (sqlite3_step(stmt) ==SQLITE_ROW) {
NSString *name = [NSStringstringWithUTF8String:sqlite3_column_text(stmt,1)];
NSString *age = [NSStringstringWithUTF8String:sqlite3_column_text(stmt,2)];
//放进模型
Contact *contact = [ContactcontactWithName:nameage:age];//
[detailArrayaddObject:contact];//加进数组
}
}
//查询的数据已经存入数组,执行时可以从数组里面取出来进行操作
9.删除
NSString *sql =@"delete from t_student where name = 'pj';";
char *errmsg;
sqlite3_exec(_db, sql.UTF8String,NULL,NULL, &errmsg);
if (errmsg) {
NSLog(@"操作失败");
}else{
NSLog(@"成功操作");
}
10,修改
NSString *sql = @"update t_student set name = 'pj';";
char *errmsg;
sqlite3_exec(_db, sql.UTF8String,NULL,NULL, &errmsg);
if (errmsg) {
NSLog(@"操作失败");
}else{
NSLog(@"成功操作");
}
2.使用第三方库fmdb
1.导入三方库
2.在Build Phases里的 Link Binary With Libraries里添加
libsqlite3.0.dylib或者libsqlite3.dylib
3.定义数据库关联
FMDatabase *_dataBase;
4.采用线程锁的方式初始化
+(DBManager *)shareManager{
staticDBManager *manager =nil;
@synchronized(self){
if (manager ==nil) {
manager = [[DBManageralloc]init];
}
}
return manager;
}
5.创建数据库
-(instancetype)init{
if (self == [superinit]) {
//路径
NSString *path = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents/user.sqlite"];
_dataBase = [[FMDatabasealloc]initWithPath:path];
BOOL isOpen = [_dataBaseopen];//打开数据库
if (isOpen) {
NSString *createSql =@"create table if not exists user (userId integer primary key autoincrement,userName varchar(255),age integer,userImage blob)";
BOOL ret = [_dataBaseexecuteUpdate:createSql];
if (ret) {
NSLog(@"success");
}else{
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}else{
NSLog(@"打开失败");
}
}
return self;
}
6.判断是否存在
-(BOOL)isExists:(NSString *)userName{
NSString *sql = @"select * from user where userName = ?";
FMResultSet *rs = [_dataBaseexecuteQuery:sql,userName];
if ([rsnext]) {
returnYES;
}
return NO;
}
7.插入数据
-(void)addUserModels:(UserModels *)model{
BOOL isExists = [selfisExists:model.userName];
if (isExists) {
NSLog(@"用户名存在");
return;
}
NSString *insertSql =@"insert into user (userName,age,userImage)values(?,?,?)";
//
NSData *imageData =UIImagePNGRepresentation(model.userImage);
BOOL ret = [_dataBaseexecuteUpdate:insertSql,model.userName,@(model.age),imageData];
if (!ret) {
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
8.查询数据
-(NSArray *)searchAllUsers{
NSString *selectSql =@"select * from user";
FMResultSet *rs = [_dataBaseexecuteQuery:selectSql];
NSMutableArray *array = [NSMutableArrayarray];
while ([rsnext]) {
UserModels *model = [[UserModelsalloc]init];
model.userId = [rsintForColumn:@"userId"];
model.userName = [rsstringForColumn:@"userName"];
model.age = [rsintForColumn:@"age"];
NSData *data = [rsdataForColumn:@"userImage"];
model.userImage = [UIImageimageWithData:data];
[arrayaddObject:model];
}
return array;
}
9.删除数据
-(void)deleteUserWithUserId:(int)userId{
NSString *deleteSql =@"delete from user where userId = ?";
BOOL ret = [_dataBaseexecuteUpdate:deleteSql];
if (!ret) {
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}
10.修改
-(void)updateUserModel:(UserModels *)model userId:(int)userId{
NSString *sql = @"update user set userName = ?,age = ?,userImage = ?where userId = ?";
NSData *data =UIImagePNGRepresentation(model.userImage);
BOOL ret = [_dataBaseexecuteUpdate:sql,model.userName,@(model.age), data,@(userId)];
if (!ret) {
NSLog(@"%@",_dataBase.lastErrorMessage);
}
}