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;

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

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

        

       if(array.count>0)

           self.comeFrom = [arrayobjectAtIndex:0];

    }

}



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

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

0 1
原创粉丝点击