iOS sqlite ORM框架-LKDBHelper

LKDBHelper

一個sqlite ORM(全自動操做數據庫)框架。
線程安全、再也不擔憂遞歸鎖死的問題git

安裝要求

  • iOS 4.3+
  • 僅支持 ARC
  • FMDB

添加到你的項目

若是你使用 CocoaPods,直接添加下面的代碼到你的 Podfile文件github

pod 'LKDBHelper' 

使用 Encryption,添加下面的代碼(順序不能錯)sql

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 方法(可選)ruby

    +(NSString *)getTableName { return @"LKTestTable"; } 
  3. 在 .m文件中重寫回調方法(可選)bash

    @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,賦值後插入數據庫app

    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」post

    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

相關文章
相關標籤/搜索