FMDB是一个封装很好的sqllite类库。项目中调用的时候只需要写SQL语句,就能实现数据的CURD。我试过即使手写SQL语句也很麻烦,需要一个字段一个字段的拼上去,而且容易出错。有没有动态获取字段的一种做法呢。当然是有的。在.NET中就有获取一个类的每个字段名称和类型之类的方法。同理,我想OC中肯定也会存在,于是乎,强大的runtime机制就可以拿来用用了。

  为什么用动态的呢,因为动态的拼接表面上就和ORM差不多了,开发者基本不用接触SQL语句,就能实现与数据库的数据交互。下面看具体介绍:

    Class c = [SomeModel Class];

    unsigned int outCount,i;
//class_copyPropertyList方法获取 SomeModel中的字段集合
objc_property_t *properties = class_copyPropertyList(c, &outCount);

  然后遍历properties,获取到每个字段的字段名称和字段类型

        objc_property_t property = properties[i];
propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; propertyType = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];

  这样,类中的每个字段都有了,类型也有了,那么拼接update语句或者insert语句再或者查询都可以。例如: UPDATE TABLE SET A='a',B='b',C=1 WHERE 1=1

  字段A,B,C已经能动态取出,a,b,1可以根据新的Model 用 [model objectForKey:]方法获取值。先简单说这么多,下面我通过一个小Demo来演示一下。

  首先实现方法,分析类的字段属性和类型,并存放到一个NSdictionary中。

- (NSDictionary *)pz_getClassPropertyWithClass:(Class)c
{
//存储列名和列类型的可变数组
NSMutableArray *propertyNames = [NSMutableArray array];
NSMutableArray *propertyTypes = [NSMutableArray array]; unsigned int outCount,i; objc_property_t *properties = class_copyPropertyList(c, &outCount); //属性名称
NSString *propertyName;
NSString *propertyType;
for (i = ; i < outCount; i ++) {
objc_property_t property = properties[i];
propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
//将不需要的排除
if ([_cloumnsNotInDB containsObject:propertyName]) {
continue;
}
[propertyNames addObject:propertyName];
//获取属性类型
propertyType = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; if ([propertyType hasPrefix:@"T@"]) {
[propertyTypes addObject:SQLTEXT];
}else if ([propertyType hasPrefix:@"Ti"]||[propertyType hasPrefix:@"TI"]||[propertyType hasPrefix:@"Ts"]||[propertyType hasPrefix:@"TS"]||[propertyType hasPrefix:@"TB"]) {
[propertyTypes addObject:SQLINTEGER];
} else {
[propertyTypes addObject:SQLREAL];
}
}
free(properties); return [NSDictionary dictionaryWithObjectsAndKeys:propertyNames,propertyNameKey,propertyTypes,propertyTypeKey, nil];
}

然后随便写个Model,调用方法如下:

 NSDictionary *dict = [[PZFMDBUtil sharedUtil] pz_getClassPropertyWithClass:[PZDBModel class]];

    NSLog(@"%@",dict);

打印结果为:

字段名称和类型都有啦,那么拼接  insert  into table (name,age,number,address) values (?,?,?,?) 是不是很easy了呢。然后在调用一下FMDB的方法,就轻松实现Model直接保存或者更新到SQLLITE中了。

例如以下代码:

    PZDBModel *model = [[PZDBModel alloc] init];
model.name = @"panzi";
model.number = ;
[[PZFMDBUtil sharedUtil] pz_addDataWithModel:model];

我们在看一下数据库:

我想,更新查询神马的就不用介绍了吧。当然呢,想在封装一层也是很麻烦的,要考虑好多东西。有时候想想还不如直接写SQL来的爽快~~

GitHub:https://github.com/fanpan26/PZFMDBHelper

最新文章

  1. Visual Studio 2013 Web开发
  2. Conversion Operators in OpenCascade
  3. xamarin for android 生成时“java.exe已退出 代码为1”
  4. liunx之:ln命令
  5. WinCE下读取注册表获得SD路径
  6. pdo知识总结
  7. 51nod 1116 K进制下的大数 (暴力枚举)
  8. Windows7下安装MongoDB
  9. JavaWeb学习总结_Servlet开发
  10. 给指针malloc分配空间后就等于数组吗?
  11. 字符串模式匹配sunday算法
  12. angular service/directive
  13. 13、Android的多线程与异步任务
  14. hyperVisors
  15. Python3 如何优雅地使用正则表达式(详解二)
  16. js判断值为null
  17. iOS 双击tabbar刷新页面
  18. #7 Python代码调试
  19. 51nod 省选联测 R2
  20. [CF1131F] Asya And Kittens

热门文章

  1. Python+Selenium定位元素的方法
  2. GreenPlum 大数据平台--运维(二)
  3. Spark-HBase集成错误之 java.lang.NoClassDefFoundError: org/htrace/Trace
  4. C++11并发编程:async,future,packaged_task,promise
  5. c#合并字典
  6. 关于Json字符串&quot;反序列化Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path...&quot;
  7. SVM之Python实现
  8. Java Web SpringMVC AJAX,实现页面懒加载数据
  9. JavaScript 监听回车事件
  10. CheckBoxList