天天看點

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

繼續閱讀