iOS開發一行代碼系列:一行搞定資料庫

來源:互聯網
上載者:User

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

 

 

對應的表結構就是 TABLE TestModel (age type,height type,distance type,isOK type,mChar type,...)

 

方法

下面是一些可用的方法,還在完善中。。。

 

 

+(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]

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.