原理

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. 实例化flask的参数及对app的配置

    Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...

  2. 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  3. x264

    x264 x264  h264  1. x264调用主要过程 x264_param_default():设置参数集结构体x264_param_t的缺省值. x264_picture_alloc():为 ...

  4. ddrmenu

    <%@ Register TagPrefix="dnn" TagName="MENU" Src="~/DesktopModules/DDRMen ...

  5. Java编译器、JVM、解释器

    Java虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该系统上运行.本文首先简要介绍从Java文件的编译 ...

  6. 2016.01.08 Javascript视频

    完成JavaScript开发视频课程的Ajax部分内容.

  7. WebAPI PUT,DELETE请求404

  8. Zabbix自带的mysql监控模块

    Zabbix自带的mysql监控模块 [root@Cagios zabbix-]# cp conf/zabbix_agentd/userparameter_mysql.conf /usr/local/ ...

  9. CAD在网页中返回当前图纸的最小外包矩形框

    主要用到函数说明: _DMxDrawX::GetMcDbDatabaseBound 返回当前图纸的最小外包矩形框,详细说明如下: 参数 说明 DOUBLE* pLbx 返回最小外包矩形框左下角X值 D ...

  10. 新版本的molar mass(uva-1586)明明debug过了,各种测试还是WA真是气死我了

    #include <bits/stdc++.h> using namespace std; double trans(string a) { stringstream ss; ss< ...