5.1基于SQLite快速开发封装库

5.1基于SQLite快速开发封装库

 

一 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;

...

 

2在.m文件重写getTableName方法

 

+(NSString *)getTableName

{

    return@"LKTestTable";

}

 

3在.m文件重写callback方法(可选)

 

@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,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'"

 

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;

    }

}

 

四 详见Demo:5.1基于SQLite快速开发封装库

github地址:https://github.com/li6185377/LKDBHelper-SQLite-ORM

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值