原理

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]

下载地址:点这里

iOS开发一行代码系列:一行搞定数据库的更多相关文章

  1. iOS开发一行代码系列:一行搞定输入框

    近期总结了下开发过程中经常使用的功能,发现有时候我在做反复性的劳动.于是决定把经常使用的功能抽出来,方便下次使用. 我的想法是:用最少的代码来解决这个问题.于是写了一些经常使用的工具类,名字就叫一行代 ...

  2. python一行代码就能搞定的事情!

    打印9*9乘法表: >>> print( '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) ...

  3. python实战===一行代码就能搞定的事情!

    打印9*9乘法表: >>> print( '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) ...

  4. 开源作品ThinkJDBC—一行代码搞定数据库操作

    1 简介 ThinkJD,又名ThinkJDBC,一个简洁而强大的开源JDBC操作库.你可以使用Java像ThinkPHP框架的M方法一样,一行代码搞定数据库操作.ThinkJD会自动管理数据库连接, ...

  5. iOS开发——实用技术OC篇&8行代码教你搞定导航控制器全屏滑动返回效果

    8行代码教你搞定导航控制器全屏滑动返回效果 前言 如果自定了导航控制器的自控制器的leftBarButtonItem,可能会引发边缘滑动pop效果的失灵,是由于 self.interactivePop ...

  6. (转载)Android快速开发偷懒必备,一句话搞定所有ViewGroup的Adapter . 支持自定义ViewGroup

    [置顶] [Android]快速开发偷懒必备,一句话搞定所有ViewGroup的Adapter . 支持自定义ViewGroup 标签: androidAdapter快速开发0耦合 2016-12-1 ...

  7. BS开发平台,一小时搞定功能强大的统计分析页面

    BS开发平台,一小时搞定功能强大的统计分析页面,拥有强大的功能和详细的权限控制. 1.组织数据,分析需求(实际耗时大约20分钟)  2.建立需要的业务数据表(大致10分钟)3. 运行代码工具,生产需要 ...

  8. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  9. 几行c#代码,轻松搞定一个女大学生

    几行c#代码,轻松搞定一个女大学生 的作业... 哈哈,标题党了哈,但是是真的,在外面敲代码,想赚点外快,接到了一个学生的期末考试,是一个天气预报的程序.程序并不难. 看到这个需求第一个想法就是只要找 ...

随机推荐

  1. 4CSS颜色和背景

    ---------------------------------------------------------------------------------------------------- ...

  2. C#最实用的快捷键

    Ctrl+J(Alt+→):智能提示. Ctrl+X:删除整行. Shift+Alt+Enter:全屏切换 F12:跳转到定义. Ctrl+-.Ctrl+Shift+-:上一步.下一步(仅限于使用过上 ...

  3. [原创]Toolbar setNavigationIcon无效

    最近在做一个Toolbar,setNavigationIcon()这个方法一直无效,说什么的都有,什么getSupportActionBar().setNavigationIcon()的,说设置sty ...

  4. 如何将本地项目上传到gitlab上?

    git push后需要输入用户名,密码 这是上传成功显示的页面 打开gitLab项目地址检查代码是否被正确上传 上传前: 上传后 这就完成将项目上传到gitlab了

  5. .htaccess使用

    RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...

  6. 无需编写代码,用接口管理工具 eoLinker 高效完成API测试流程相关业务

    引言 作为开发人员,有时需要协助市场部门进行开发,比如在市场推广中,经常通过给用户赠送优惠券来提高复购率.这篇文章,将介绍如何使用接口管理工具 eoLinker 的自动化测试 [UI模式] 构建此业务 ...

  7. iview表单密码自定义验证

    From中定义   ref="passwordForm" 获取dom节点  :model="passwordForm" 关联表单数据对象 :rules=&quo ...

  8. 解决Webpack中提示syntax 'classProperties' isn't currently enabled的错误

    当我们使用了一些JavaScript的一些新特性的时候,但是有没有在webpack.config.js里面或者是.babelrc文件中配置相关插件,就可以解决了. error:Support for ...

  9. MySQL5.7本地首次登录win10报错修改

    1.打开MySQL目录下的my.ini文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件.(Win10默认安装,my.ini在C:\ProgramData\MySQL\M ...

  10. os、sys模块

    os模块 os模块是与操作系统交互的一个接口 os.makedirs("dirname1/dirname2") # 可生成多层递归目录 os.removedirs("di ...