iOS LKDBHlper对数据库的使用(包含:外键关联)

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];

       return self.comeFrom.ID;

    }

    return nil;

}

// 重载    从数据库中  获取的值  经过自己处理 再保存

-(void)userSetValueForModel:(LKDBProperty *)property value:(id)value

{

   if([property.sqlColumnNameisEqualToString:@"comeFrom"])

    {

       self.comeFrom =nil;

        //IDint的时候 ID= %d  不需要单引号

       NSMutableArray* array  = [CountrysearchWithWhere:[NSStringstringWithFormat:@"ID='%@'",value]orderBy:niloffset:0 count:1];

        

       if(array.count>0)

           self.comeFrom = [arrayobjectAtIndex:0];

    }

}



userSetValueForModel方法 其实就是根据外键ID找到对应的model 然后返回这个model

demo地址  http://download.csdn.net/detail/aa741649143/7700515

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值