5.1基于SQLite快速开发封装库

5.1基于sqlite快速开发封装库

一 LKDBHelper简介

LKDBHelper是github上开源的数据库操作封装库。

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

二 如何引用LKDBHelper

*必备环境

iOS 4.3+

ARC only

FMDB(https://github.com/ccgus/fmdb)

1 可以使用pod导入,pod'LKDBHelper','~> 2.1.9'。

2 手动导入,依赖于FMDB,添加libsqlite3.dylib。

三LKDBHelper使用说明

1创建一个Model

@interfaceLKTest : NSObject

@property(copy,nonatomic)NSString* name;

@propertyNSUInteger age;

@propertyBOOL isGirl;

@property(strong,nonatomic)LKTestForeign* address;

@property(strong,nonatomic)NSArray* blah;

@property(strong,nonatomic)NSDictionary* hoho;

@propertychar like;

...

2在.m文件重写getTableName方法

+(NSString *)getTableName

{

return@"LKTestTable";

}

3在.m文件重写callback方法(可选)

@interfaceNSObject(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,插入数据库

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":[NSDatedate]};

//异步插入第一条数据 Insert the first

[test saveToDB];

//or

//[globalHelper insertToDB:test];

5 select查询

NSMutableArray* array = [LKTest searchWithWhere:nilorderBy:niloffset:0count:100];

for (id obj in arraySync) {

addText(@"%@",[obj printAllPropertys]);

}

6 delete(删除

[LKTest deleteToDB:test];

7 update(更新)

test.name = "rename";

[LKTest updateToDB:test where:nil];

8 isExists(是否存在)

[LKTest isExistsWithModel:test];

9 rowCount(函数

[LKTest rowCountWithWhere:nil]; }

10 查询条件举例

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,3) and sex=0 " or @{@"rowid":@[@1,@3],@"sex":@0}

If you want to be judged,only use NSString

For example: @"date >= '2013-04-01 00:00:00'"

11 table mapping表映射)

+(NSDictionary *)getTableMapping

{

//return nil

return @{@"name":LKsqlInherit,

@"MyAge":@"age",

@"img":LKsqlInherit,

@"MyDate":@"date",

@"color":LKsqlInherit,

@"address":LKsqlUserCalculate};

}

12 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 = [NSStringstringWithFormat:@"update %@ set error = name",tableName];

[db executeUpdate:sql];

}];

}

}

}

13 set columnattribute (option)(设置列名)

+(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:5.1基于sqlite快速开发封装库

github地址:https://github.com/li6185377/LKDBHelper-sqlite-ORM

相关文章

安装 在Windows上安装SQLite。 访问官网下载下Precompliled Binaries for Windows的两个压缩包。 创建s...
一、安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries for Windows下的包下...
实例: 会员信息管理 功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员  ...
关于SQLite SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的在读写效率、消耗总量、延迟时间和整...