zoukankan      html  css  js  c++  java
  • 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]

    下载地址:点这里

  • 相关阅读:
    Java——Math,Set,List,map相关练习
    Java——单例模式、多线程
    Java——I/O入门相关练习代码
    Java——I/O相关练习代码
    Java——序列化与反序列化
    python-selenium-粘贴,删除,复制和悬停
    Typora图床设置(阿里云版,图片自动上传)
    图书管理系统(Java实现,十个数据表,含源码、ER图,超详细报告解释,2020.7.11更新)
    openresty的安装和使用
    工具丨超好用的免费AWR分析工具
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5220098.html
Copyright © 2011-2022 走看看