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

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能