一 LKDBHelper简介
LKDBHelper是github上开源的数据库操作封装库。
全面支持 NSArray,NSDictionary,ModelClass,NSNumber,NSString,NSDate,NSData,UIColor,UIImage,CGRect,CGPoint,CGSize,NSRange,int,char,float,double,long.. 等属性的自动化操作(插入和查询)。
二 如何引用LKDBHelper
*必备环境
•iOS 4.3+
•ARC only
•FMDB(https://github.com/ccgus/fmdb)
1 可以使用pod导入,pod'LKDBHelper','~> 2.1.9'。
2 手动导入,依赖于FMDB,添加libsqlite3.dylib。
三LKDBHelper使用说明
1创建一个Model
@interfaceLKTest : NSObject @property(copy,nonatomic)NSString* name; @propertyNSUInteger age; @propertyBOOL isGirl;
@property(strong,nonatomic)LKTestForeign* address; @property(strong,nonatomic)NSArray* blah; @property(strong,nonatomic)NSDictionary* hoho;
@propertychar like; ... |
+(NSString *)getTableName { return@"LKTestTable"; } |
@interfaceNSObject(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 |
4 初始化Model,插入数据库
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":[NSDatedate]};
//异步插入第一条数据 Insert the first [test saveToDB]; //or //[globalHelper insertToDB:test]; |
5 select(查询)
NSMutableArray* array = [LKTest searchWithWhere:nilorderBy:niloffset:0count:100]; for (id obj in arraySync) { addText(@"%@",[obj printAllPropertys]); } |
6 delete(删除)
[LKTest deleteToDB:test]; |
7 update(更新)
test.name = "rename"; [LKTest updateToDB:test where:nil]; |
8 isExists(是否存在)
[LKTest isExistsWithModel:test]; |
9 rowCount(函数)
[LKTest rowCountWithWhere:nil]; } |
10 查询条件举例
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,3) and sex=0 " or @{@"rowid":@[@1,@3],@"sex":@0}
If you want to be judged,only use NSString For example: @"date >= '2013-04-01 00:00:00'" |
11 table mapping(表映射)
+(NSDictionary *)getTableMapping { //return nil return @{@"name":LKsqlInherit, @"MyAge":@"age", @"img":LKsqlInherit, @"MyDate":@"date", @"color":LKsqlInherit, @"address":LKsqlUserCalculate}; } |
12 table update(option)(表更新)
+(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 = [NSStringstringWithFormat:@"update %@ set error = name",tableName]; [db executeUpdate:sql]; }]; } } } |
13 set columnattribute (option)(设置列名)
+(void)columnAttributeWithProperty:(LKDBProperty *)property { if([property.sqlColumnName isEqualToString:@"MyAge"]) { property.defaultValue = @"15"; } if([property.propertyName isEqualToString:@"date"]) { property.isUnique = YES; property.checkValue = @"MyDate > '2000-01-01 00:00:00'"; property.length = 30; } } |
github地址:https://github.com/li6185377/LKDBHelper-sqlite-ORM