LKDBHelper的使用
Demo下载地址
https://github.com/potato512/SYCacheManager
https://github.com/li6185377/LKDBHelper-SQLite-ORM
LKDBHelper可以不设置sql语句的情况下,仅使用自定义Model就可以全自动的进行数据表的创建,及数据的插入、查询、更新、删除等操作。
支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate,NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float,double, long.. 等属性的自动化操作(插入和查询)。
直接创建数据库、数据表和对数据的操作,而无需打开数据库、关闭数据。
常用方法
1、创建实例LKDBHelper
- (instancetype)initWithDBName:(NSString *)dbname;
- (instancetype)initWithDBPath:(NSString *)filePath;
2、表操作
(1)创建表
- (BOOL)createTableWithModelClass:(Class)modelClass;
(2)删除表
- (void)dropAllTable;
- (BOOL)dropTableWithClass:(Class)modelClass;
- (BOOL)dropTableWithTableName:(NSString *)tableName;
3、数据操作
(1)插入数据
- (BOOL)insertToDB:(NSObject *)model;
- (void)insertToDB:(NSObject *)modelcallback:(void (^)(BOOL result))block;
插入model的主键primary key不存在时才会插入
- (BOOL)insertWhenNotExists:(NSObject *)model;
- (void)insertWhenNotExists:(NSObject *)model callback:(void (^)(BOOLresult))block;
(2)修改数据
- (BOOL)updateToDB:(NSObject *)modelwhere:(id)where;
- (void)updateToDB:(NSObject *)modelwhere:(id)where callback:(void (^)(BOOL result))block;
(3)查找数据(where格式:"name = 'zhang'";orderBy格式:升序 "name asc",降序 "name desc";offset偏移量:10表示从第11个开始;count查找数量:10表示查找10个)
- (NSMutableArray*)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(NSInteger)offset count:(NSInteger)count;
- (void)search:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy offset:(NSInteger)offset count:(NSInteger)count callback:(void (^)(NSMutableArray* array))block;
- (id)searchSingle:(Class)modelClass where:(id)where orderBy:(NSString*)orderBy;
(4)删除数据
- (BOOL)deleteToDB:(NSObject *)model;
- (void)deleteToDB:(NSObject *)modelcallback:(void (^)(BOOL result))block;
示例代码:
注意加入头文件#import "LKDBHelper.h"
设置数据库文件路径
- (NSString *)filePath
{
if (!_filePath)
{
// document目录下
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *document = [documentArray objectAtIndex:0];
_filePath = [document stringByAppendingPathComponent:SQLiteFile];
}
NSLog(@"filePath %@", _filePath);
return _filePath;
}
实例化(懒加载)
- (LKDBHelper *)dbHelper
{
if (!_dbHelper)
{
_dbHelper = [[LKDBHelper alloc] initWithDBPath:self.filePath];
[_dbHelper dropAllTable];
}
return _dbHelper;
}
创建新表
- (void)New
{
BOOL result = [self.dbHelper createTableWithModelClass:[LKDBModel class]];
if (result)
{
NSLog(@"创建表成功");
}
else
{
NSLog(@"创建表失败");
}
}
插入数据
- (void)Insert
{
LKDBModel *model = [[LKDBModel alloc] init];
model.name = @"zhangshaoyu";
model.nickName = @"大番薯";
model.phone = @"13800138000";
model.adderss = @"深圳";
model.education = @"大学";
model.company = @"公司1";
model.job= @"iOS研发";
BOOL result = [self.dbHelper insertToDB:model];
if (result)
{
NSLog(@"1 插入数据成功");
}
else
{
NSLog(@"2 插入数据失败");
}
// 或
LKDBModel *model2 = [[LKDBModel alloc] init];
model2.name = @"zhangshaoyu";
model2.nickName = @"大番薯";
model2.phone = @"13800138000";
model2.adderss = @"深圳福田";
model2.education = @"大学";
model2.company = @"公司2";
model2.job = @"iOS研发";
[self.dbHelper insertToDB:model2 callback:^(BOOL result) {
if (result)
{
NSLog(@"2 插入数据成功");
}
else
{
NSLog(@"2 插入数据失败");
}
}];
}
修改更新数据
- (void)Update
{
// 查找条件
NSString *where = @"company = '公司2'";
NSString *orderBy = @"company asc"; // "name desc""
// NSArray *array = [self.dbHelper search:[LKDBModel class] where:whereorderBy:orderBy offset:0 count:100];
// if (array && 0 != array.count)
// {
// for (LKDBModel *model in array)
// {
// NSLog(@"更新 查找数据成功name=%@,nickName=%@,phone=%@,address=%@,education=%@,company=%@,job=%@",model.name,model.nickName,model.phone,model.adderss,model.education,model.company,model.job);
// }
// }
// else
// {
// NSLog(@"更新查找数据失败");
// }
// 或
[self.dbHelper search:[LKDBModel class] where:where orderBy:orderByoffset:0 count:100 callback:^(NSMutableArray *array) {
if (array && 0 != array.count)
{
for (LKDBModel *model in array)
{
NSLog(@"更新 查找数据成功name=%@,nickName=%@,phone=%@,address=%@,education=%@,company=%@,job=%@",model.name,model.nickName,model.phone,model.adderss,model.education,model.company,model.job);
if ([model.companyisEqualToString:@"公司1"])
{
self.userModel = model;
}
}
}
else
{
NSLog(@"更新查找数据失败");
}
}];
if (self.userModel)
{
self.userModel.name = @"zhangshaoyu";
self.userModel.job = @"APP研发";
// BOOL result = [self.dbHelper updateToDB:self.userModel where:nil];
// if (result)
// {
// NSLog(@"更新数据成功");
// }
// else
// {
// NSLog(@"更新数据失败");
// }
// 或
[self.dbHelper updateToDB:self.userModel where:nil callback:^(BOOLresult) {
if (result)
{
NSLog(@"更新数据成功");
}
else
{
NSLog(@"更新数据失败");
}
}];
}
else
{
NSLog(@"更新数据失败");
}
}
查找数据
- (void)Select
{
// 查找条件
NSString *where = nil; // @"company = '公司2'";
NSString *orderBy = nil; // @"company asc"; // "namedesc""
// NSArray *array = [self.dbHelper search:[LKDBModel class] where:whereorderBy:orderBy offset:0 count:100];
// if (array && 0 != array.count)
// {
// for (LKDBModel *model in array)
// {
// NSLog(@"查找数据成功name=%@,nickName=%@,phone=%@,address=%@,education=%@,company=%@,job=%@",model.name,model.nickName,model.phone,model.adderss,model.education,model.company,model.job);
// }
// }
// else
// {
// NSLog(@"查找数据失败");
// }
// 或
[self.dbHelper search:[LKDBModel class] where:where orderBy:orderByoffset:0 count:100 callback:^(NSMutableArray *array) {
if (array && 0 != array.count)
{
for (LKDBModel *model in array)
{
NSLog(@"查找数据成功name=%@,nickName=%@,phone=%@,address=%@,education=%@,company=%@,job=%@",model.name,model.nickName,model.phone,model.adderss,model.education,model.company,model.job);
if ([model.companyisEqualToString:@"公司1"])
{
self.userModel = model;
}
}
}
else
{
NSLog(@"查找数据失败");
}
}];
}
删除数据
- (void)Delete
{
// BOOL result = [self.dbHelper deleteToDB:self.userModel];
// if (result)
// {
// NSLog(@"更新数据成功");
// }
// else
// {
// NSLog(@"更新数据失败");
// }
// 或
[self.dbHelper deleteToDB:self.userModel callback:^(BOOL result) {
if (result)
{
NSLog(@"删除数据成功");
}
else
{
NSLog(@"删除数据失败");
}
}];
}
删除表
- (void)DeleteAll
{
BOOL result = [self.dbHelper dropTableWithClass:[LKDBModel class]];
if (result)
{
NSLog(@"删除表成功");
}
else
{
NSLog(@"删除表失败");
}
}
数据查看
注意事项:
1、使用过程中,无需关注数据库的打开,关闭。
2、条件设置的格式处理。如:
where格式:"name= 'zhang'"
orderBy格式:按名字升序 "name asc",或按名字降序 "name desc"
offset格式:从第11个开始查找:10
count格式:只查找10个:10