Latest 1.0.3
Homepage https://github.com/wyhazq/YHDB
License MIT
Platforms ios 6.0, requires ARC
Dependencies FMDB
Frameworks Foundation
Authors

Package based on fmdb,used to conveniently call database operation.

Install

Pod
pod 'YHDB', '~> 1.0.3'
Download

download the YHDB & fmdb

import

#import "NSObject+YHDB.h"

BASE

Perform the protocol in Model if you need,example:
@implementation

//one DB:if you want to modify the db name,default is "YHDB".
- (NSString *)dbName {
    return @"YHDB";
}

//multiple DB:For example,you can userId.
- (NSString *)dbName {
    return [NSString stringWithFormat:@"%ld", (long)self.userId];
}

//have primaryKey
- (NSString *)primaryKey {
    return @"a";
}

//Perform if you need ensure a row or some rows while primaryKey is AutoIncrement
//自增主键时,如果需要唯一确定一行或者几行时用
- (NSArray *)whereKeysForPrimaryKeyAutoIncrement {
    return @[@"c"];
}
@end

Save

Automatic matching for insert or update
1.a model
Model *model = [[Model alloc] init];
model.a = 1;
model.b = @"a"
model.c = 1.1;

[model save];//save data to database
2.a lot of models
Model *model0 = [[Model alloc] init];
model0.a = 1;
model0.b = @"a"
model0.c = 1.1;

Model *model1 = [[Model alloc] init];
model1.a = 2;
model1.b = @"b"
model1.c = 2.2;

[Model save:@[model0, model1]];//save data to database

Insert

[model insert];
[Model insert:@[model0, model1]];
[Model insert:@"sql"];
[Model insert:@[sql0,sql1,...];

Delete

//1.delete from Model;
[[Model deleteSelf] executeDelete];

//2.delete from Model where a = 1
[[[Model deleteSelf] where:@"a = 1"] executeDelete];
[[[Model deleteSelf] where:@{@"a" : @(1)}] executeDelete];

//3.delete from Model where a in (1,2,3)
[[[Model deleteSelf] whereIn:@"a in (1,2,3)"] executeDelete];
[[[Model deleteSelf] whereIn:@{@"a" : @[@(1), @(2), @(3)]}] executeDelete];

Update

[model update];
[Model update:@[model0, model1]];
[Model update:@"sql"];
[Model update:@[sql0,sql1,...];

Select

//1.select one row to model
Model *model = [[Model alloc] initWithPK:@(1)];

//2.select a lot row to model
NSArray *array = [Model selectModelsWithSql:@"select * from Model"];
NSArray *array = [[Model select:@"*"] executeQuery];
//and...
/**choose what you need:
 *where:     @{key : obj} || @"key = obj" 
 *           [... where:@{@"a" : @(1)}]; [... where:@"a = 1"];
 *whereIn:   @{key : @[obj]} || @"key in (obj)" 
 *           [... whereIn:@"a in (1,2,3)"]; [... whereIn:@{@"a" : @[@(1), @(2), @(3)]}];
 *groupBy:   @[key] || @"key" 
 *           [... groupBy:@"a"]; [... groupBy:@[a]];
 *having:    @"string" [... having:@"a > 1"];
 *orderBy:   @{@"ASC" : @[key]}, @{@"DESC" : @[key]} || @"key ASC", @"key DESC" 
 *           [... orderBy:@"a ASC"]; [... orderBy:@{@"ASC" : @[@"a"]];
 *limit:     0, size [... limit:0, 10];
 */

 //3.select count
 NSInteger count = [[Model selectCount] executeQueryCount];

executeUpdate

[Model executeUpdateWithSql:@"sql"];

executeQuery

NSArray *array = [Model executeQueryWithSql:@"sql"];

Other

resetDB
[Model resetDB];
create Index
[Model createIndexOnColumn:@"a"];
drop Index
[Model dropIndexOnColumn:@"a"];
alter Column
@interface Model
//add a property
@property (nonatomic, strong)NSString *d;
@end

[Model alterTableAddColumn:@"d"];
drop table
[Model drop];

Latest podspec

{
    "name": "YHDB",
    "version": "1.0.3",
    "summary": "Package based on fmdb.",
    "description": "Package based on fmdb,used to call database operation conveniently.used on iOS, which implement by Objective-C.",
    "homepage": "https://github.com/wyhazq/YHDB",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "wyhazq": "[email protected]"
    },
    "platforms": {
        "ios": "6.0"
    },
    "source": {
        "git": "https://github.com/wyhazq/YHDB.git",
        "tag": "1.0.3"
    },
    "source_files": "YHDB",
    "frameworks": "Foundation",
    "requires_arc": true,
    "dependencies": {
        "FMDB": []
    }
}

Pin It on Pinterest

Share This