• Stars
    star
    1,207
  • Rank 38,822 (Top 0.8 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

全自动的插入,查询,更新,删除, an automatic database operation thread-safe and not afraid of recursive deadlock

LKDBHelper

this is sqlite ORM (an automatic database operation)
thread-safe and not afraid of recursive deadlock

简书:不定时更新 http://www.jianshu.com/users/376b950a20ec

Big Upgrade 2.0

Supported NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. attribute to insert and select automation.

全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor, UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)


Requirements

Adding to your project

If you are using CocoaPods, then, just add this line to your Podfile

pod 'LKDBHelper'

If you are using encryption, Order can not be wrong

pod 'FMDB/SQLCipher'
pod 'LKDBHelper'

@property(strong,nonatomic)NSString* encryptionKey;

Basic usage

  1. Create a new Objective-C class for your data model
@interface LKTest : NSObject
@property (nonatomic, copy) NSURL *url;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSUInteger age;
@property (nonatomic, assign) BOOL isGirl;

@property (nonatomic, strong) LKTestForeign *address;
@property (nonatomic, strong) NSArray *blah;
@property (nonatomic, strong) NSDictionary *hoho;

@property (nonatomic, assign) char like;
...
  1. in the *.m file, overwirte getTableName function (option)
+ (NSString *)getTableName {
    return @"LKTestTable";
}
  1. in the *.m file, overwirte callback function (option)
@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
  1. Initialize your model with data and insert to database
    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];
    
  1. select 、 delete 、 update 、 isExists 、 rowCount ...
    select:
        
        NSMutableArray *array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
        for (id obj in array) {
            addText(@"%@",[obj printAllPropertys]);
        }
        
    delete:
        
        [LKTest deleteToDB:test];
        
    update:
        
        test.name = "rename";
        [LKTest updateToDB:test where:nil];
        
    isExists:
        
        [LKTest isExistsWithModel:test];
    
    rowCount:
        
        [LKTest rowCountWithWhere:nil];
        
     
  1. Description of parameters "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'"

table mapping

overwirte getTableMapping Function (option)

//手动or自动 绑定sql列
+ (NSDictionary *)getTableMapping {
    return @{ @"name" : LKSQL_Mapping_Inherit,
              @"MyAge" : @"age",
              @"img" : LKSQL_Mapping_Inherit,
              @"MyDate" : @"date",
              
              // version 2 after add
              @"color" : LKSQL_Mapping_Inherit,
              
              //version 3 after add
              @"address" : LKSQL_Mapping_UserCalculate,
              @"error" : LKSQL_Mapping_Inherit
              };
}

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 = [NSString stringWithFormat:@"update %@ set error = name", tableName];
                [db executeUpdate:sql];
            }];
        }
    }
    LKErrorLog(@"your know %@", columns);
}

set column attribute (option)

// 定制化列属性
+ (void)columnAttributeWithProperty:(LKDBProperty *)property {
    if ([property.sqlColumnName isEqualToString:@"MyAge"]) {
        property.defaultValue = @"15";
    } else if ([property.propertyName isEqualToString:@"date"]) {
        // if you use unique,this property will also become the primary key
        //        property.isUnique = YES;
        property.checkValue = @"MyDate > '2000-01-01 00:00:00'";
        property.length = 30;
    }
}

demo screenshot

demo screenshot
table test data

foreign key data


Use in swift

Remember to override the class function getTableName for model.

Change-log

Version 1.1 @ 2012-6-20

  • automatic table mapping
  • support optional columns
  • support column attribute settings
  • you can return column content

Version 1.0 @ 2013-5-19

  • overwrite and rename LKDBHelper
  • property type support: UIColor,NSDate,UIImage,NSData,CGRect,CGSize,CGPoint,int,float,double,NSString,short,char,bool,NSInterger..
  • fix a recursive deadlock.
  • rewrite the asynchronous operation -
  • thread-safe
  • various bug modified optimize cache to improve performance
  • test and demos
  • bug fixes, speed improvements

Version 0.0.1 @ 2012-10-1

  • Initial release with LKDAOBase

License

This code is distributed under the terms and conditions of the MIT license.


Contribution guidelines

  • if you are fixing a bug you discovered, please add also a unit test so I know how exactly to reproduce the bug before merging

Contributors

Author: Jianghuai Li

Contributors: waiting for you to join

More Repositories

1

IMYAOPTableView

无业务入侵,无逻辑入侵,外部察觉不到的 UITableView/UICollectionView AOP 框架
Objective-C
467
star
2

IMYWebView

Replace UIWebView to WKWebView
Objective-C
292
star
3

IMYWebLoader

支持对 UIWebView/WKWebView 的资源, 进行 预加载、缓存、拦截 等操作 ... Orz
Objective-C
234
star
4

IMYADLaunchDemo

开屏广告Demo,以非同一般的方式接入广告
Objective-C
183
star
5

DouQu_iOS

Leancloud BaaS Demo,前后端全包
Objective-C
166
star
6

WaterDropRefresh

path waterdrop refresh effect 水滴刷新效果
Objective-C
132
star
7

IMYAppGrayStyle

App一键全局变灰
Objective-C
126
star
8

SDWebImage-Category

UIImageView add download progress bar and click on the download fails automatic retry
Objective-C
109
star
9

LKAlarmManager

方便快捷的把 “您的提醒” 加入到 日历或者本地通知中
Objective-C
77
star
10

LKDaoBase

automation database operation 根据绑定的实体类对数据库自动操作(增,删,改,查) use fmdb
Objective-C
27
star
11

ClearUnusedImage

删除xcode项目中未使用的图片
Objective-C
25
star
12

AddScore2

simple animation demo use for add scoresimple animation demo use for add score,similar health point view
Objective-C
20
star
13

Chinese-replace-and-extract

代码中文搜索,替换并提取 到.string 文件中 ,用于应用的多语言准备
Objective-C
14
star
14

IMYViewCache

对View进行缓存 预加载UIView,提高界面切换速度。 支持全局UITableViewCell复用
Objective-C
12
star
15

LKCarlendarView

simple and quick‘ calendar
Objective-C
9
star
16

IMYHiveMind

一个 iOS 控制反转框架(IoC、依赖注入、依赖查找、控制反转)
Objective-C
7
star
17

IMYAsyncBlock

Objective-C
2
star
18

xib-link-zhongwen

自动提取xib 上的中文控件 并 初始化 设置文本 用于多语言准备
Objective-C
2
star
19

FuckKit

An iOS base Kit
Objective-C
1
star
20

AddScore

ios simple animation demo
Objective-C
1
star