LKDBHelper
来源:互联网 发布:o2o源码 编辑:程序博客网 时间:2024/06/13 08:02
LKDBHelper
this is sqlite ORM (an automatic database operation)
thread-safe and not afraid of recursive deadlock
QQ群号 113767274 有什么问题或者改进的地方大家一起讨论
推荐个 json 和 model 互转非常好用的工具类 https://github.com/dcty/YYJSON 支持 NSData 直接转换成 model array
Big Upgrade 2.0
Supported NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor,UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. attribute to insert and select automation.
全面支持 NSArray,NSDictionary, ModelClass, NSNumber, NSString, NSDate, NSData, UIColor,UIImage, CGRect, CGPoint, CGSize, NSRange, int,char,float, double, long.. 等属性的自动化操作(插入和查询)
Requirements
- iOS 4.3+
- ARC only
- FMDB(https://github.com/ccgus/fmdb)
Adding to your project
If you are using CocoaPods, then, just add this line to your PodFile
pod 'LKDBHelper', :head
If you are using Encryption, Order can not be wrong
pod 'FMDB/SQLCipher',:headpod 'LKDBHelper',:head
@property(strong,nonatomic)NSString* encryptionKey;
Basic usage
1 . Create a new Objective-C class for your data model
@interface LKTest : NSObject@property(copy,nonatomic)NSString* name;@property NSUInteger age;@property BOOL isGirl;@property(strong,nonatomic)LKTestForeign* address;@property(strong,nonatomic)NSArray* blah;@property(strong,nonatomic)NSDictionary* hoho;@property char like;...
2 . in the *.m file, overwirte getTableName function (option)
+(NSString *)getTableName{ return @"LKTestTable";}
3 . in the *.m file, overwirte callback function (option)
@interface NSObject(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 . Initialize your model with data and insert to database
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":[NSDate date]}; ///warning: NSDate没做处理 所以不能在 NSArray 里 或者 NSDictionry 里面使用NSDate //异步 插入第一条 数据 Insert the first [test saveToDB]; //or //[globalHelper insertToDB:test];
5 . select 、 delete 、 update 、 isExists 、 rowCount ...
select: NSMutableArray* array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100]; for (id obj in arraySync) { addText(@"%@",[obj printAllPropertys]); } delete: [LKTest deleteToDB:test]; update: test.name = "rename"; [LKTest updateToDB:test where:nil]; isExists: [LKTest isExistsWithModel:test]; rowCount: [LKTest rowCountWithWhere:nil];
6 . Description of parameters "where"
For example: 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,2,3) and sex=0 " or @{@"rowid":@[@1,@2,@3],@"sex":@0} If you want to be judged , only use NSString For example: @"date >= '2013-04-01 00:00:00'"
table mapping
overwirte getTableMapping Function (option)
+(NSDictionary *)getTableMapping{ //return nil return @{@"name":LKSQLInherit, @"MyAge":@"age", @"img":LKSQLInherit, @"MyDate":@"date", @"color":LKSQLInherit, @"address":LKSQLUserCalculate};}
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 = [NSString stringWithFormat:@"update %@ set error = name",tableName]; [db executeUpdate:sql]; }]; } }}
set column attribute (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 screenshot
table test data
foreign key data
- LKDBHelper
- LKDBHelper 数据库位置
- LKDBHelper-ORM框架
- LKDBHelper简单介绍和使用
- 数据缓存——LKDBHelper的使用
- 结合SQLCipher的LKDBHelper数据库加密
- LKDBHelper 使用FMDB 对数据库的自动操作
- ios - LKDBHelper - sqlite 数据库 自动化操作 工具类 v1.1版 更新
- LKDBHelper的各种接口使用方式,及各种sql组合条件语句。
- 第十一周项目三:程序的多文件组织
- 传奇私服在风雨中陈烂
- 第十一周项目4-特殊的三位数
- String 查找函数c++
- 在存储过程中查询某个关键字
- LKDBHelper
- oracle ctxsys用户 ,driimp对象
- 求绝对值_JAVA_杭电ACM2003
- 1-11-4 - 特殊三位数(参考精华版)
- OpenCV中parallel_for 和 parallel_for_学习笔记
- Android拍照并保存(对图片进行压缩,分解)
- AlertDialog自定义标题大小、颜色、位置
- ios高效开发二--ARC跟block那点事
- GDB调试手册