LKDBHelper
一个sqlite ORM(全自动操作数据库)框架。
线程安全、不再担心递归锁死的问题
安装要求
添加到你的项目
如果你使用 CocoaPods,直接添加下面的代码到你的 Podfile文件
pod 'LKDBHelper'
pod 'FMDB/sqlCipher'
pod 'LKDBHelper'
@property(strong,nonatomic)Nsstring* encryptionKey;
基础用法
-
创建一个 Objective-C类,作为 data model
@interface LKTest : NSObject @property(copy,nonatomic)Nsstring* name; @property NSUInteger age; @property BOOL isgirl; @property(strong,nonatomic)LKTestForeign* address; @property(strong,nonatomic)NSArray* blah; @property(strong,nonatomic)NSDictionary* hoho; @property char like; ...
-
+(Nsstring *)getTableName { return @"LKTestTable"; }
-
@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
-
初始化你的 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":[NSDate date]}; //同步 插入第一条 数据 Insert the first [test savetoDB]; //or //[globalHelper insertToDB:test];
-
select、delete、update、isExists、rowCount ...
select: NSMutableArray* array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100]; for (id obj in arraySync) { 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 方法(可选)
+(NSDictionary *)getTableMapping
{
//return nil
return @{@"name":LKsqlInherit,
@"MyAge":@"age",
@"img":LKsqlInherit,
@"MyDate":@"date",
@"color":LKsqlInherit,
@"address":LKsqlUserCalculate};
}
更新表(可选)
+(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];
}];
}
}
}
设置列属性(可选)
+(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;
}
}
demo 截屏
测试表数据
外键数据
github 原文地址
https://github.com/li6185377/LKDBHelper-SQLite-ORM
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。