Code Monkey home page Code Monkey logo

fmdbhelper's Introduction

FMDBHelper

Stars Version License Platform

Installation

FMDBHelper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'FMDBHelper', '1.1.0'

Performance

support for sqlite rw.

Usage

  • Add #import "FMDBHelper.h" to your prefix.pch
  • And [FMDBHelper setDataBaseName:@"demo.db"];

Example

if you have a table like this:

user (
id text PRIMARY KEY,
name text,
age integer,
birthday text,
dept text,
dogs text,
nums text
)

or have a JSON like this:

{
"id": "a1b2c3d4e5",
"username": "李京城",
"age": 32,
"birthday": "1984/3/28",
"dept":{
"id": "f6g7h8i9j0",
"name": "dev",
"manager": "X"
},
"dogs":[{
"id": "0x0x0x0x",
"name": "ahuang",
"age": "15"
}, {
"id": "1x1x1x1x",
"name": "xhei",
"age": "6"
}
],
"nums":[
123,
234
]
}

Create a model class and declare properties, property name must be consistent with the field names in a database table, but it doesn't have to be consistent with json keys.

//User.h
@interface User : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) NSString *birthday;
@property (nonatomic, strong) Dept *dept;
@property (nonatomic, copy) NSArray<Dog *> *dogs;
@property (nonatomic, copy) NSArray<NSNumber *> *nums;

@end

//User.m
@implementation User

// if the property name and the JSON keys is different, you need to overwrite this method.
- (NSDictionary *)mapping {
return @{ @"username": @"name" };
}

// if the property type is a custom class, you need to overwrite this method.
- (NSDictionary *)objectPropertys {
return @{ @"dept": [Dept class] };
}

// If the property type is a NSArray<...>, and the property type is a custom class, you need to overwrite this method.
- (NSDictionary *)genericForArray {
return @{ @"dogs": [Dog class] };
}

// if the model class name and the table name is different, you need to overwrite this method
+ (NSString *)tableName {
return @"sys_user";
}

@end

Read JSON and insert it into the table.

NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"json"];
NSDictionary *keyValues = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:NSJSONReadingMutableLeaves error:nil];

User *user = [[User alloc] initWithDictionary:keyValues];

[FMDBHelper insertObject:user];

Query data from the table and initialize the model object

NSDictionary *result = [FMDBHelper queryById:@"a1b2c3d4e5" from:[User tableName]];

User *user = [[User alloc] initWithDictionary:result];

NSLog(@"%@, %@, %ld, %@", user.ID, user.name, (long)user.age, user.birthday);
NSLog(@"%@, %@, %@", user.dept.ID, user.dept.name, user.dept.manager);

NSArray<Dog *> *dogs = user.dogs;

[dogs enumerateObjectsUsingBlock:^(Dog * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@,%@,%ld", obj.ID, obj.name, obj.age);
}];

More usage reference test case.

Author

李京城

License

FMDBHelper is available under the MIT license. See the LICENSE file for more info.

fmdbhelper's People

Contributors

lijingcheng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.