OC 中数据持久化的方案一般为: plist文件,用户偏好设置,归档,sqllite/core data. 对于较大的数据量一般采用 sqlite 或 core data,归档效率太低,而core data 是对 sqlite 的oc 封装,性能上直接使用 sqlite 更优.只需要编写一些简单的 sqlite 语句就可进行 CRUD 操作.
直接上代码:
(eg: 一个对象有name,filename,icon...等等属性,假如现在要将用户的收藏对象保存数据库进行 CRUD 操作)
模型:
@interface ZMMusicModel : NSObject<NSCoding>
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (copy, nonatomic) NSString *filename;
@property (copy, nonatomic) NSString *lrcname;
@property (copy, nonatomic) NSString *singer;
@property(nonatomic,copy) NSString* singerIcon;
-(instancetype) initWithDict:(NSDictionary*)dict;
+(instancetype) musicWithDiet:(NSDictionary*)dict;
+(NSArray*)plist;
/*!
* 添加数据
*/
-(void)insertMusic;
/*!
* 移除数据
*/
-(void)deleteMusic;
/*!
* 用户当前选中的歌曲是否已经收藏
*/
-(BOOL)isCollect;
@end
#import "ZMMusicModel.h"
#import "YCollectManager.h"
@implementation ZMMusicModel
-(instancetype) initWithDict:(NSDictionary*)dict
{
self=[super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype) musicWithDiet:(NSDictionary*)dict
{
return [[self alloc] initWithDict:dict];
}
+(NSArray*)plist
{
NSString* paths=[[NSBundle mainBundle] pathForResource:@"Musics.plist" ofType:nil];
NSArray* array=[NSArray arrayWithContentsOfFile:paths];
NSMutableArray* arrayM=[[NSMutableArray alloc] init];
for (NSDictionary* tmp in array) {
[arrayM addObject:[self musicWithDiet:tmp]];
}
return arrayM;
}
// 添加数据
-(void)insertMusic
{
NSString* strSQL = [NSString stringWithFormat:@"INSERT INTO t_student (name, icon,filename,lrcname,singer,singerIcon) VALUES ('%@', '%@', '%@', '%@', '%@', '%@')",self.name,self.icon,self.filename,self.lrcname,self.singer,self.singerIcon];
if([[YCollectManager shareInstance] execSQL:strSQL]){
NSLog(@"数据插入成功");
}
}
// 删除数据
-(void)deleteMusic
{
NSString* strSQL = [NSString stringWithFormat:@"DELETE FROM t_student WHERE name = '%@'",_name];
if([[YCollectManager shareInstance] execSQL:strSQL])
{
NSLog(@"数据删除成功");
}else{
NSLog(@"数据删除失败");
}
}
// 2.判断是否已经收藏数据
-(BOOL)isCollect
{
NSString* strSQL = [NSString stringWithFormat:@"SELECT name, filename FROM t_student WHERE name = '%@'",self.name];
// 2.执行查询语句
NSArray *dictArray = [[YCollectManager shareInstance] querySQL:strSQL];
return dictArray.count > 0 ? YES : NO;
}
@end
封装收藏工具类:
.h 文件
@interface YCollectManager : NSObject
+(instancetype)shareInstance;
// 打开数据库
-(BOOL)OpenSQL;
-(BOOL)execSQL:(NSString*)strSQL;
/*!
* 查询数据库中的所有数据
*/
- (NSArray *)querySQL:(NSString *)querySQL;
/*!
* 获取所有用户收藏的数据
*/
-(NSArray*)collect;
@end
.m文件
#import "YCollectManager.h"
#import "ZMMusicModel.h"
#import <sqlite3.h>
@interface YCollectManager()
@property(nonatomic,assign)sqlite3* db;
@end
@implementation YCollectManager
static YCollectManager* _instance;
+(instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[YCollectManager alloc] init];
});
return _instance;
}
-(BOOL)OpenSQL
{
NSString* fileName = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
fileName = [fileName stringByAppendingPathComponent:@"music.sqlite"];
if(sqlite3_open(fileName.UTF8String,&_db) != SQLITE_OK){
return NO;
}
// 能来到这里就是打开成功 创表
return [self creatTable];
}
-(BOOL)creatTable
{
NSString* strSQL = @"CREATE TABLE IF NOT EXISTS 't_student' ('id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,'name' TEXT,'icon' TEXT ,'filename' TEXT ,'lrcname' TEXT ,'singer' TEXT ,'singerIcon' TEXT )";
return [self execSQL:strSQL];
}
-(BOOL)execSQL:(NSString*)strSQL
{
return sqlite3_exec(self.db, strSQL.UTF8String, nil, nil, nil) == SQLITE_OK;
}
- (NSArray *)querySQL:(NSString *)querySQL
{
// 定义游标对象
sqlite3_stmt *stmt = nil;
if (sqlite3_prepare_v2(self.db, querySQL.UTF8String, -1, &stmt, nil) != SQLITE_OK) {
NSLog(@"准备查询失败");
return nil;
};
// 准备成功,开始查询数据
NSMutableArray *dictArray = [NSMutableArray array];
while (sqlite3_step(stmt) == SQLITE_ROW) {
int count = sqlite3_column_count(stmt);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i = 0; i < count; i++) {
const char *cKey = sqlite3_column_name(stmt, i);
NSString *key = [NSString stringWithUTF8String:cKey];
const char *cValue = (const char *)sqlite3_column_text(stmt, i);
NSString *value = [NSString stringWithUTF8String:cValue];
[dict setValue:value forKey:key];
}
[dictArray addObject:dict];
}
return dictArray;
}
/**ZMMusicModel 为对象模型 */
-(NSArray*)collect
{
NSString* strSQL = @"SELECT name, icon,filename,lrcname,singer,singerIcon FROM t_student";
// 2.执行查询语句
NSArray *dictArray = [[YCollectManager shareInstance] querySQL:strSQL];
// 将字典转模型
NSMutableArray* arrM = [NSMutableArray array];
for (NSDictionary* dict in dictArray) {
ZMMusicModel* model = [[ZMMusicModel alloc] initWithDict:dict];
[arrM addObject:model];
}
return arrM;
}
@end