IOS CoreData 多表查询(下)

摘要:
对于多表查询,表之间必须存在某种关系。对于外部联接和左联接等操作,COREDATA较弱。在上一节中,我们介绍了数据库的关系查询操作。下面使用CoreData演示关系数据库中表之间的关系。COREDATA的生成以及如何设置关系将不再详细讨论。请参阅上一篇文章。

http://blog.csdn.net/fengsh998/article/details/8123392

iOS CoreData中,多表查询上相对来说,没有SQL直观,但COREDATA的功能还是可以完成相关操作的。

多表查询,表与表之间肯定存在某种关系,如果对于类似外连接,左连接等操作,在COREDATA中就显得无力(请高手指教了)。

在上节中,介绍了一下数据库的关系查询操作。

下面使用CoreData进行关系数据库的表与表之间的关系演示。

生成COREDATA和如何设置关系就不再详谈了,见之前的文章。

建立好的关系图:

IOS CoreData 多表查询(下)第1张

一步步建立上面关系图:

先建立部门表,员工表,职位表,工资等级表,开户银行表

IOS CoreData 多表查询(下)第2张

IOS CoreData 多表查询(下)第3张

IOS CoreData 多表查询(下)第4张

IOS CoreData 多表查询(下)第5张

IOS CoreData 多表查询(下)第6张

上面建立表之后,我们还需要建立表之间的关系

部门和员工之间的关系:1 V  N

IOS CoreData 多表查询(下)第7张

部门和职位的关系:1  V  N

IOS CoreData 多表查询(下)第8张

IOS CoreData 多表查询(下)第9张

员工与职位的关系:多对一关系

IOS CoreData 多表查询(下)第10张

IOS CoreData 多表查询(下)第11张

员工与开户行:一个员工只能提供一个开户行,但一个开户行可以给多名员工进行开卡。所以关系为N V 1;

IOS CoreData 多表查询(下)第11张

IOS CoreData 多表查询(下)第13张

职位和工资等级:一个职位只对应一个工资等级;1V1

IOS CoreData 多表查询(下)第14张

IOS CoreData 多表查询(下)第15张

下面插入测试数据:

- (IBAction)onbtnclick:(id)sender
{
    Department *dept = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
    dept.dp_deptname = @"HR";
    
    Department *dept2 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];

    dept2.dp_deptname = @"DEV";
    
    Department *dept3 = [NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:self.managedObjectContext];
    dept3.dp_deptname = @"POD";
    
    
    Salary *sy =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
    sy.sy_level = @"D";
    sy.sy_scale = [NSNumber numberWithDouble:0.1];
    
    Salary *sy2 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
    sy2.sy_level = @"C";
    sy2.sy_scale = [NSNumber numberWithDouble:0.15];
    
    Salary *sy3 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
    sy3.sy_level = @"B";
    sy3.sy_scale = [NSNumber numberWithDouble:0.4];
    
    Salary *sy4 =[NSEntityDescription insertNewObjectForEntityForName:@"Salary" inManagedObjectContext:self.managedObjectContext];
    sy4.sy_level = @"A";
    sy4.sy_scale = [NSNumber numberWithDouble:0.8];
    
    Post *pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt.pt_name = @"行政专员";
    pt.dept = dept;
    
    Post *pt2= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];    
    pt2.salary = sy3;
    pt2.pt_name = @"人事经理";
    pt2.dept = dept;
    
    Post *pt3= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt3.pt_name = @"开发工程师";
    pt3.dept = dept2;
    pt3.salary = sy2;
    
    pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt.pt_name = @"架构师";
    pt.dept = dept2;
    
    pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];    
    pt.pt_name = @"项目经理";
    pt.dept =dept2;
    
    Post *pt6= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];    
    pt6.pt_name = @"测试工程师";
    pt6.dept = dept2;
    pt6.salary = sy;
    
    pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt.pt_name = @"销售代表";
    pt.dept = dept3;
    
    pt= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt.pt_name = @"销售经理";
    pt.dept = dept3;
    
    Post *pt9= [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:self.managedObjectContext];
    pt9.pt_name = @"大客户经理";
    pt9.dept = dept3;
    pt9.salary = sy4;
    
    
    Bank *bk = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
    bk.bk_name = @"招行";
    bk.bk_address = @"广州";
    
    Bank *bk2 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
    bk2.bk_name = @"浦发";
    bk2.bk_address = @"上海";
    
    Bank *bk3 = [NSEntityDescription insertNewObjectForEntityForName:@"Bank" inManagedObjectContext:self.managedObjectContext];
    bk3.bk_name = @"工行";
    bk3.bk_address = @"深圳";
    
    Employee *em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    em.em_age = [NSNumber numberWithInt:20];
    em.em_name = @"张三";
    em.em_sex = [NSNumber numberWithInt:1];
    em.em_bankcardid = @"46326587439043";
    em.dept = dept2;
    em.post = pt3;
    em.bank = bk2;
    
    em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    em.em_age = [NSNumber numberWithInt:18];
    em.em_name = @"李四";
    em.em_sex = [NSNumber numberWithInt:2];
    em.em_bankcardid = @"32565443246567";
    em.dept = dept;
    em.post = pt2;
    em.bank = bk3;
    
    em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];

    em.em_age = [NSNumber numberWithInt:26];
    em.em_name = @"欧阳";
    em.em_sex = [NSNumber numberWithInt:2];
    em.em_bankcardid = @"14354654656767";
    em.dept = dept3;
    em.post = pt9;
    em.bank = bk3;
    
    em = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    em.em_age = [NSNumber numberWithInt:22];
    em.em_name = @"欧阳";
    em.em_sex = [NSNumber numberWithInt:2];
    em.em_bankcardid = @"9873429837433";
    em.dept = dept2;
    em.post = pt6;
    em.bank = bk3;
    
    [self.managedObjectContext save:nil];
}

数据库中数据:

IOS CoreData 多表查询(下)第16张

IOS CoreData 多表查询(下)第17张

IOS CoreData 多表查询(下)第18张

IOS CoreData 多表查询(下)第19张

IOS CoreData 多表查询(下)第20张

1、查询开发部门中名为张三的工资等级

NSEntityDescription * emEty = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *frq = [[[NSFetchRequest alloc]init]autorelease];
    
    [frq setEntity:emEty];
    
    NSPredicate * cdt = [NSPredicate predicateWithFormat:@"em_name = %@",@"张三"];
    
    [frq setPredicate:cdt];
    
    NSArray *objs =[self.managedObjectContext executeFetchRequest:frq error:nil];
    
    //NSLog(@"%i",[objs count]);
    NSLog(@"%@",((Employee*)[objs objectAtIndex:0]).post.salary.sy_level);

2、查询运维部名为欧阳的工资等级及开户银行

 NSEntityDescription * entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *fetch = [[[NSFetchRequest alloc]init]autorelease];
    
    [fetch setEntity:entity];
    
    NSPredicate * qcmd = [NSPredicate predicateWithFormat:@"em_name = %@ ",@"欧阳"];
    
    [fetch setPredicate:qcmd];
    
    NSArray * obs = [self.managedObjectContext executeFetchRequest:fetch error:nil];
    
    NSPredicate * filter = [NSPredicate predicateWithFormat:@"dept.dp_deptname = %@",@"POD"];
    
    NSArray * ret = [obs filteredArrayUsingPredicate:filter];  //从数组中进行过滤。
    
    NSLog(@"%@",((Employee*)[ret objectAtIndex:0]).post.salary.sy_level);

多表查询主要就在于表之间建立好相关的关联关系(relationship),其次就是充分的使用NSPredicate 这个查询条件来进行过滤。

免责声明:文章转载自《IOS CoreData 多表查询(下)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇在ThreadPool.QueueUserWorkIte 的回调函数中发生未处理异常导致了应用程序重启QT下的darknet-GPU项目属性下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WordPress主题制作教程10:添加文章类型插件Custom Post Type UI

下载 Custom Post Type UI>> 用Custom Post Type UI添加自定义文章类型对于新手来说最简单不过了,下载安装后,在插件栏启用一下,就可以开始添加文章类型了 添加后,可以看见左栏多了“产品”: 你还可以创建完类型后,在CPT UI菜单下的import/Export->Get Code的这段代码贴到fun...

Android Volley框架的几种post提交请求方式

首先简单描写叙述一下Google的Android开发团队在2013年推出的一个网络通信框架Volley.它的设计目标是进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比方下载文件等,Volley的表现就不尽如人意。 在app开发中,我们最常见的就是从appclient向服务端发一个http请求.对于两种主要的web请求方式get和post...

C# Post form-data 上传

1.首先先从NuGet下载包  2.方法 public string PostData(string UrlAdd, string inputData){ string str = ""; try { HttpClient client = ne...

GET和POST请求区别

关于http协议GET和POST方法的区别我们可以从各处得到比较一致的答案,今天我们来填一个面试中可能碰到的一个坑。 当面试官问你“你觉得GET和POST有什么区别"时,我们可能会想到以下几点(来源于网络): GET在浏览器回退时是无害的,而POST会再次提交请求。 GET产生的URL地址可以被Bookmark,而POST不可以。 GET请求会被浏览器主...

Python接口自动化-接口基础(二)

一、HTTP请求方式 1.常见请求方式 方法 描述 GET 请求指定的页面信息,并返回实体主体 HEAD 类似于 GET 请求,只不过返回的响应中没有具体的内容,用于获取报头 POST 向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST 请求可能会导致新的资源的建立和/或已有资源的修改 PUT 从...

Python Django 前后端数据交互 之 HttpRequest、HttpResponse、render、redirect

在使用三神装的时候,首先当然是得要导入它们: from django.shortcuts import HttpResponse, render, redirect   一、HttpRequest捕获请求 捕获请求——HttpRequest对象 1、属性 HttpRequest.scheme  #一个字符串,表示请求的方案(通常是http或者https)H...