iOS sqlite ORM框架-LKDBHelper

LKDBHelper

一个sqlite ORM(全自动操作数据库)框架。
线程安全、不再担心递归锁死的问题

安装要求

  • iOS 4.3+
  • 仅支持 ARC
  • FMDB

添加到你的项目

如果你使用 CocoaPods,直接添加下面的代码到你的 Podfile文件

    pod 'LKDBHelper'

使用 Encryption,添加下面的代码(顺序不能错)

    pod 'FMDB/SQLCipher'
    pod 'LKDBHelper'

@property(strong,nonatomic)NSString* encryptionKey;

基础用法

  1. 创建一个 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;
     ...
    
  2. 在 .m文件中重写 getTableName 方法(可选)

     +(NSString *)getTableName
     {
         return @"LKTestTable";
     }
    
  3. 在 .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
    
  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":[NSDate date]};
    
     //同步 插入第一条 数据   Insert the first
     [test saveToDB];
     //or
     //[globalHelper insertToDB:test];
    
  5. 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];
    
  6. 参数描述 “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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,842评论 0 9
  • 1.对一个运营来说,要是认知和思维意识都还没跟上,直接奔着方法和技巧去,那你很快就可能走火入魔。 2.在互联网行业...
    褪了色的美瞳阅读 1,358评论 0 0
  • 每一天早上老公都会催我起床,我都是被迫起床,命令自己快一点快一点,我心里好压抑,我不想起,好像我的心还没有舒展,老...
    徐恺嵘阅读 1,534评论 0 0
  • 你,在我身边吗? 独自。 一人…… 走过了,变不在再感到厌烦了! 那一次次的相逢成了最中的回忆了。
    十字体坛阅读 916评论 0 1