iOS开发一行代码系列:一行搞定数据库
原理
iOS 和 SQL的对应关系
Model类结构 => SQL表结构
Model实例 => SQL表中的一行
Model实例的属性 => SQL表中的一列
Model和Table的对应
@interface TestModel :NSObject @property (assign, nonatomic) NSInteger age; @property (assign, nonatomic) CGFloat height; @property (assign, nonatomic) long distance; @property (assign, nonatomic) double maxDistance; @property (assign, nonatomic) BOOL isOK; @property (assign, nonatomic) char mChar; @property (assign, nonatomic) CGRect rect; @property (assign, nonatomic) CGPoint point; @property (assign, nonatomic) CGSize size; @property (assign, nonatomic) NSRange range; @property (strong, nonatomic) NSNumber *number; @property (strong, nonatomic) NSString *string; @property (strong, nonatomic) UIColor *color; @property (strong, nonatomic) NSDate *date; @property (strong, nonatomic) NSData *data;// convert to data @property (strong, nonatomic) NSValue *value; @property (strong, nonatomic) UIImage *image; @end
方法
下面是一些可用的方法,还在完善中。。。
+(ModelToTableMap*)getModelToTableMap; +(NSString*)getDBPath; +(NSString*)getTableName; +(NSArray*)getPrimaryKeys; +(NSDictionary *)getPropertyToColumnMap; +(NSDictionary *)getDefaultValueDictionary; +(NSDictionary *)getLengthDictionary; +(NSDictionary *)getCheckValueDictionary; +(NSDictionary *)getIsUniqueDictionary; +(NSDictionary *)getIsNotNullDictionary; +(BOOL)shouldModelToTableMapContainParentProperties; +(BOOL)shouldModelToTableMapContainSelfProperties; +(NSString *)getDateFormatterString; +(NSString *)getDBImagePathWithImageName:(NSString *)imgName ; +(NSString *)getDBDataPathWithDataName:(NSString *)dataName; +(BOOL)createTable; +(BOOL)dropTable; +(id)firstModelWhere:(NSObject *)where; +(id)firstModelWhere:(NSObject *)where orderBy:(NSString*)orderBy ; +(NSArray *)allModels; +(NSArray *)findModelsWhere:(NSObject *)where; +(NSArray *)findModelsWhere:(NSObject *)where orderBy:(NSString*)orderBy offset:(int)offset count:(int)count; +(BOOL)insertModel:(NSObject *)model; +(BOOL)insertModelWhenNotExists:(NSObject *)model; +(BOOL)deleteModel:(NSObject *)model; +(BOOL)deleteModelsWhere:(NSObject *)where; +(BOOL)updateModelsWithModel:(NSObject *)model where:(NSObject *)where; +(BOOL)updateModelsWithDictionary:(NSDictionary *)dic where:(NSObject *)where; +(BOOL)isModelExists:(NSObject *)model; - (BOOL)saveModel; - (BOOL)deleteModel; - (BOOL)updateModel:(NSObject *)theNewModel; - (BOOL)updateModelWithDictionary:(NSDictionary *)dic;
使用
TestModel *model = [[TestModel alloc] init]; model.age = 20; model.image = [UIImage imageNamed:@"img.jpg"];
//增加 [model saveModel]
//删除 [model deleteModel]
//修改 [model updateModelWithDictionary:@{@"age":@(21)}]
//查找 [TestModel allModels]
下载地址:点这里
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。