LKDBHelper
这是 sqlite ORM(自动数据库操作)
线程安全,不怕递归死锁
简书:不定时更新 http://www.jianshu.com/users/376b950a20ec
大版本升级 2.0
支持 NSArray,NSDictionary,ModelClass,NSNumber,NSString,NSDate,NSData,UIColor,UIImage,CGRect,CGPoint,CGSize,NSRange,int,char,float,double,long 等属性的插入和选择自动化。
全面支持 NSArray,NSDictionary,ModelClass,NSNumber,NSString,NSDate,NSData,UIColor,UIImage,CGRect,CGPoint,CGSize,NSRange,int,char,float,double,long 等属性的自动化操作(插入和查询)。
需求
- iOS 4.3+
- 仅 ARC
- FMDB(https://github.com/ccgus/fmdb)
将项目添加到您的项目中
如果您使用CocoaPods,那么只需在您的Podfile中添加此行
pod 'LKDBHelper'
如果使用加密,顺序不能错误
pod 'FMDB/SQLCipher'
pod 'LKDBHelper'
@property(strong,nonatomic)NSString* encryptionKey;
基本用法
- 为您的数据模型创建一个新的Objective-C类
@interface LKTest : NSObject
@property (nonatomic, copy) NSURL *url;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSUInteger age;
@property (nonatomic, assign) BOOL isGirl;
@property (nonatomic, strong) LKTestForeign *address;
@property (nonatomic, strong) NSArray *blah;
@property (nonatomic, strong) NSDictionary *hoho;
@property (nonatomic, assign) char like;
...
- 在*.m文件中,覆盖getTableName函数(可选)
+ (NSString *)getTableName {
return @"LKTestTable";
}
- 在*.m文件中,覆盖回调函数(可选)
@interface NSObject (LKDBHelper_Delegate)
+ (void)dbDidCreateTable:(LKDBHelper *)helper tableName:(NSString *)tableName;
+ (void)dbDidAlterTable:(LKDBHelper *)helper tableName:(NSString *)tableName addColumns:(NSArray *)columns;
+ (BOOL)dbWillInsert:(NSObject *)entity;
+ (void)dbDidInserted:(NSObject *)entity result:(BOOL)result;
+ (BOOL)dbWillUpdate:(NSObject *)entity;
+ (void)dbDidUpdated:(NSObject *)entity result:(BOOL)result;
+ (BOOL)dbWillDelete:(NSObject *)entity;
+ (void)dbDidDeleted:(NSObject *)entity result:(BOOL)result;
///data read finish
+ (void)dbDidSeleted:(NSObject *)entity;
@end
- 使用数据初始化您模型并将其插入到数据库中
LKTestForeign *foreign = [[LKTestForeign alloc] init];
foreign.address = @":asdasdasdsadasdsdas";
foreign.postcode = 123341;
foreign.addid = 213214;
//插入数据 insert table row
LKTest *test = [[LKTest alloc] init];
test.name = @"zhan san";
test.age = 16;
//外键 foreign key
test.address = foreign;
test.blah = @[@"1", @"2", @"3"];
test.blah = @[@"0", @[@1] ,@{ @"2" : @2 }, foreign];
test.hoho = @{@"array" : test.blah, @"foreign" : foreign, @"normal" : @123456, @"date" : [NSDate date]};
//同步 插入第一条 数据 Insert the first
[test saveToDB];
//or
//[globalHelper insertToDB:test];
- 选择、删除、更新、判断是否存在、行数...
select:
NSMutableArray *array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
for (id obj in array) {
addText(@"%@",[obj printAllPropertys]);
}
delete:
[LKTest deleteToDB:test];
update:
test.name = "rename";
[LKTest updateToDB:test where:nil];
isExists:
[LKTest isExistsWithModel:test];
rowCount:
[LKTest rowCountWithWhere:nil];
- "where"参数的描述
For example:
single: @"rowid = 1" or @{ @"rowid" : @1 }
more: @"rowid = 1 and sex = 0" or @{ @"rowid" : @1, @"sex" : @0 }
when where is "or" type , such as @"rowid = 1 or sex = 0"
you only use NSString
array: @"rowid in (1,2,3)" or @{ @"rowid" : @[@1, @2, @3] }
composite: @"rowid in (1,2,3) and sex=0 " or @{ @"rowid" : @[@1, @2, @3], @"sex" : @0}
If you want to be judged , only use NSString
For example: @"date >= '2013-04-01 00:00:00'"
表映射
覆盖getTableMapping函数(可选)
//手动or自动 绑定sql列
+ (NSDictionary *)getTableMapping {
return @{ @"name" : LKSQL_Mapping_Inherit,
@"MyAge" : @"age",
@"img" : LKSQL_Mapping_Inherit,
@"MyDate" : @"date",
// version 2 after add
@"color" : LKSQL_Mapping_Inherit,
//version 3 after add
@"address" : LKSQL_Mapping_UserCalculate,
@"error" : LKSQL_Mapping_Inherit
};
}
表更新(可选)
// 表结构更新回调
+ (void)dbDidAlterTable:(LKDBHelper *)helper tableName:(NSString *)tableName addColumns:(NSArray *)columns {
for (int i = 0; i < columns.count; i++) {
LKDBProperty *p = [columns objectAtIndex:i];
if ([p.propertyName isEqualToString:@"error"]) {
[helper executeDB:^(FMDatabase *db) {
NSString *sql = [NSString stringWithFormat:@"update %@ set error = name", tableName];
[db executeUpdate:sql];
}];
}
}
LKErrorLog(@"your know %@", columns);
}
设置列属性(可选)
// 定制化列属性
+ (void)columnAttributeWithProperty:(LKDBProperty *)property {
if ([property.sqlColumnName isEqualToString:@"MyAge"]) {
property.defaultValue = @"15";
} else if ([property.propertyName isEqualToString:@"date"]) {
// if you use unique,this property will also become the primary key
// property.isUnique = YES;
property.checkValue = @"MyDate > '2000-01-01 00:00:00'";
property.length = 30;
}
}
演示截图
Swift 中使用
请记住重写模型类的 getTableName
方法。
变更日志
版本 1.1 @ 2012-6-20
- 自动表映射
- 支持可选列
- 支持列属性设置
- 你可以返回列内容
版本 1.0 @ 2013-5-19
- 重写并重命名 LKDBHelper
- 属性类型支持:UIColor,NSDate,UIImage,NSData,CGRect,CGSize,CGPoint,int,float,double,NSString,short,char,bool,NSInterger..
- 修复递归死锁。
- 重写异步操作 -
- 线程安全
- 修改各种错误,优化缓存以提升性能
- 测试和演示
- 错误修复,速度提升
版本 0.0.1 @ 2012-10-1
- 与 LKDAOBase 一起发布
许可
此代码在 MIT 许可证的条款和条件下分发。
贡献指南
- 如果你正在修复你发现的错误,请添加一个单元测试,这样在我们合并之前,我可以确切地知道如何重现错误
贡献者
作者:李江淮
贡献者:期待你的加入