iOS LKDBHlper对数据库的使用(包含:外键关联)
来源:互联网 发布:淘宝棉拖鞋女 编辑:程序博客网 时间:2024/06/14 20:11
1.准备
第三方:LKDBHelper
导入libsqlite.dylib
Country 类 作为 Car的属性外键
//// Car.h// demo//// Created by linpeng on 14-7-30.// Copyright (c) 2014年 linpeng. All rights reserved.//#import <Foundation/Foundation.h>#import "LKDBHelper.h"@interface Country : NSObject/** * 名字 */@property(nonatomic,strong)NSString *name;/** * ID */@property(nonatomic,strong)NSString *ID;@end//===============================@interface Car : NSObject<NSCoding,NSCopying,NSMutableCopying>//重载可以选择 使用的LKDBHelper+(LKDBHelper *)getUsingLKDBHelper;/** * CID */@property(nonatomic,strong)NSString *CID;/** * 颜色 */@property(nonatomic,strong)NSString *color;/** * 中国制造 */@property(nonatomic,strong)NSString *madeBy;/** * 进口国 */@property(nonatomic,strong)Country *comeFrom;@end
//// Car.m// demo//// Created by linpeng on 14-7-30.// Copyright (c) 2014年 linpeng. All rights reserved.//#import "Car.h"@implementation Country+(NSString *)getTableName{ return @"CountryTable";}+(NSString *)getPrimaryKey{ return @"ID";}@end@implementation Car+(NSString *)getTableName{ return @"CarTable";}+(NSString *)getPrimaryKey{ return @"CID";}/** * 外键关联得时候需要实现以下三个方法 * * @param 外键关联得时候必须实现 * * *///在类 初始化的时候(外键关联得时候必须实现)+(void)initialize{ //remove unwant property //比如 getTableMapping 返回nil 的时候 会取全部属性 这时候 就可以 用这个方法 移除掉 不要的属性 [self removePropertyWithColumnName:@"error"]; [self setUserCalculateForCN:@"comeFrom"];}// 重载 返回自己处理过的 要插入数据库的值-(id)userGetValueForModel:(LKDBProperty *)property{ if([property.sqlColumnName isEqualToString:@"comeFrom"]) { if(self.comeFrom == nil) return @""; [Country insertToDB:self.comeFrom]; return self.comeFrom.ID; } return nil;}// 重载 从数据库中 获取的值 经过自己处理 再保存-(void)userSetValueForModel:(LKDBProperty *)property value:(id)value{ if([property.sqlColumnName isEqualToString:@"comeFrom"]) { self.comeFrom = nil; //当ID为int 的时候 ID= %d 不需要单引号 NSMutableArray* array = [Country searchWithWhere:[NSString stringWithFormat:@"ID='%@'",value] orderBy:nil offset:0 count:1]; if(array.count>0) self.comeFrom = [array objectAtIndex:0]; }}@end
2.调用
//// LPViewController.m// demo//// Created by linpeng on 14-7-30.// Copyright (c) 2014年 linpeng. All rights reserved.//#import "LPViewController.h"#import "Car.h"@interface LPViewController ()@end@implementation LPViewController- (void)viewDidLoad{ [super viewDidLoad]; Car *car = [[Car alloc] init]; car.color = @"red"; car.madeBy = @"linpeng"; car.CID = @"10000"; Country *c = [[Country alloc] init]; c.name = @"中国"; c.ID = @"10022"; car.comeFrom = c; if (![[LKDBHelper getUsingLKDBHelper] isExistsModel:car]) { [[LKDBHelper getUsingLKDBHelper]createTableWithModelClass:[Car class]]; [[LKDBHelper getUsingLKDBHelper]createTableWithModelClass:[Country class]]; } [Car insertToDB:car]; NSArray *arr = [Car searchColumn:nil where:nil orderBy:nil offset:0 count:100]; for (int i = 0; i<arr.count; i++) { Car *car = arr[i]; NSLog(@"====%@=====%@===%@=\n",car.color,car.madeBy,car.comeFrom.ID); } }- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
3.结果
4.注意
要实现外键关联得到对应的model 则必须要实现以下三个方法(必须实现)
/**
* 外键关联得时候需要实现以下三个方法
*
* @param 外键关联得时候必须实现
*
*
*/
//在类 初始化的时候(外键关联得时候必须实现)
+(void)initialize
{
//remove unwant property
//比如 getTableMapping返回nil的时候 会取全部属性 这时候 就可以 用这个方法 移除掉 不要的属性
[selfremovePropertyWithColumnName:@"error"];
[selfsetUserCalculateForCN:@"comeFrom"];
}
// 重载 返回自己处理过的 要插入数据库的值
-(id)userGetValueForModel:(LKDBProperty *)property
{
if([property.sqlColumnNameisEqualToString:@"comeFrom"])
{
if(self.comeFrom ==nil)
return@"";
[Country insertToDB:self.comeFrom];
returnself.comeFrom.ID;
}
return nil;
}
// 重载 从数据库中 获取的值 经过自己处理再保存
-(void)userSetValueForModel:(LKDBProperty *)property value:(id)value
{
if([property.sqlColumnNameisEqualToString:@"comeFrom"])
{
self.comeFrom =nil;
//当ID为int的时候 ID= %d 不需要单引号
NSMutableArray* array = [CountrysearchWithWhere:[NSStringstringWithFormat:@"ID='%@'",value]orderBy:niloffset:0count:1];
if(array.count>0)
self.comeFrom = [arrayobjectAtIndex:0];
}
}
demo地址 http://download.csdn.net/detail/aa741649143/7700515
- iOS LKDBHlper对数据库的使用(包含:外键关联)
- Linq从两个edmx(两个数据库)中选取数据进行查找出现错误解决 (指定的 LINQ 表达式包含对与不同上下文关联的查询)
- iOS 关联对象的使用
- iOS 关联函数的使用
- hibernate多对一关联映射(cascade的使用)
- hibernate多对一关联映射(cascade的使用)
- hibernate多对一关联映射(cascade的使用)
- 数据库外键关联的注意事项
- iOS中使用FMDB对数据库的操作
- 多对多关联关系的使用
- iOS中关联对象的简单使用(objc_setAssociatedObject)
- iOS中关联对象的简单使用(objc_setAssociatedObject)
- iOS对数据库的操作
- mysq中使用sql查看数据库中所有表的外键关联情况
- 指定的 LINQ 表达式包含对与不同上下文关联的查询的引用。
- 指定的 LINQ 表达式包含对与不同上下文关联的查询的引用
- 指定的 LINQ 表达式包含对与不同上下文关联的查询的引用。
- hibernate简单程序,实现从页面对数据库的增删改查(主从表关联)
- 完整性约束(章节摘要)
- 编译器的编译基本过程
- Python核心编程学习笔记-Python核心编程第七章
- 无序关联容器(C++11)
- <Android>两个布局之间的跳转
- iOS LKDBHlper对数据库的使用(包含:外键关联)
- hdu 循环多少次
- 深入理解Http协议
- POJ 2533--Longest Ordered Subsequence
- hdu 4902 Nice boat 2014年多校联合第四场
- POJ2002-Squares
- poj-2777
- cp
- Elasticsearch & Logstash -- 一些经验总结