• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

BCSqliteORM_FMDB: 利用objective-c的runtime特性,结合现有的操作sqlite的FMDB库,实 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

BCSqliteORM_FMDB

开源软件地址:

https://gitee.com/BlockCheng/BCSqliteORM_FMDB

开源软件介绍:

BCSqliteORM v1.0

an objective-c ORM base on FMDB(https://github.com/ccgus/fmdb) and objective-c runtime.

Usage

Setup

I am using FMDB as SQLite wrapper.

Implement BCORMEntityProtocol

make your model entity implement BCORMEntityProtocol protocol

@interface ClassEntity : NSObject<BCORMEntityProtocol>@property (nonatomic,assign)NSInteger classId;@property (nonatomic,copy)NSString* className;@end
@implementation ClassEntity- (NSString *)description{    return [NSString stringWithFormat:@"[Class:id(%ld) name:(%@)]:%p",self.classId,self.className,self];}+(NSString*)tableName{    return @"class";}+(NSDictionary*)tableEntityMapping{    return @{ @"classId":BCSqliteTypeMakeIntPrimaryKey(@"id", YES),              @"className":BCSqliteTypeMakeTextDefault(@"name", NO,@"Software01")              };}@end

it will build a table like this.or just make a mapping输入图片说明

it will build a sql like this:

CREATE TABLE class (  id integer PRIMARY KEY AUTOINCREMENT NOT NULL,  name text NOT NULL DEFAULT('Software01'));

Open OR create a database file

you can create a new datase file ,or open an existing file like this

BCORMHelper* helper = [[BCORMHelper alloc]initWithDatabaseName:@"test.db" 				           enties: @[ [ClassEntity class],[StudentEntity class]]];

OR

BCORMHelper* helper = [[BCORMHelper alloc]													initWithDatabasePath:@"/Users/BlockCheng/Library/Application Support/test.db" 			enties: @[ [ClassEntity class],[StudentEntity class]]];

Save an Model

an example model:

ClassEntity* classeEntity = [ClassEntity new];classeEntity.className = @"Software02";classeEntity.classId = 2;StudentEntity* student = [StudentEntity new];student.age = 12;student.score = 80;student.classId = 2;student.studentNum = 421125;student.studentName = @"BlockCheng";
[helper save:classeEntity];[helper save:student];

query a model

BCSqlParameter *queryParam  = [[BCSqlParameter  alloc] init];queryParam.entityClass = [StudentEntity class];queryParam.propertyArray = @[@"age",@"classId",@"score",@"studentName",@"studentNum"];queryParam.selection = @"classId = ? and studentNum=?";queryParam.selectionArgs = @[@1,@421128];queryParam.orderBy = @" studentNum  asc";id entity  = [helper queryEntityByCondition:queryParam];NSLog(@"entity:----%@",entity);

OR simply use

entity  = [helper queryEntityByCondition:BCQueryParameterMake([StudentEntity class],						@[@"age",@"classId",@"score",@"studentName",@"studentNum"],@"classId = ? and studentNum=?",						@[@1,@421128], 						@" studentNum  asc", nil, -1, -1)];NSLog(@"entity:----%@",entity);

query many models

NSArray* entities  = [helper queryEntitiesByCondition:				BCQueryParameterMake([ClassEntity class],				nil, @"classId = ?", @[@1],				nil, nil, -1, -1)];NSLog(@"entities:----%@",entities);

OR

BCSqlParameter *queryParam  = [[BCSqlParameter  alloc] init];queryParam.entityClass = [StudentEntity class];queryParam.selection = @"classId = ?";queryParam.selectionArgs = @[@1];NSArray* entities  = [helper queryEntitiesByCondition:queryParam];

update

[helper update:student];    

OR with a update condition

[helper updateByCondition:BCUpdateParameterMake([StudentEntity class],@"studentName=?", @[@"new_name"],@"studentNum=?", @[@421125])];

delete

[helper remove:entity];    

OR with delete condition

[helper deleteByCondition:BCDeleteParameterMake([StudentEntity class],@"studentNum < ?", @[@421135])];    

architecture输入图片说明

License

The license for BCSqliteORM is contained in the "License.txt" file.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap