gpt4 book ai didi

ios - 在 swift 中编写连接查询 DBAccess

转载 作者:行者123 更新时间:2023-11-29 00:59:42 26 4
gpt4 key购买 nike

我想获取如下结构的数据。

Display Record

//像这样的媒体表::-

import UIKit

@objc(Media)

class Media: DBObject {
dynamic var media_id : NSNumber!;
dynamic var post_id : Post!;
dynamic var desc : String!;
dynamic var url : String!;
}

我想从“Post”表中获取数据,并从“Media”表中获取媒体列表。

一个帖子可能有多个媒体它可能不是(媒体列表零)。

请建议 DBAccess 查询以在 Swift 中获取 DBResultSet。

最佳答案

您可以向查询添加一个参数,joinTo

这是头文件中的条目。

/**
* Used to include "joined" data within the query string, you must use the tablename.columnname syntax within a where statement

*
* @param (Class)joinTo the class you would like to pwrform a SQL JOIN with
* @param (NSString*)leftParameter the property name that you would like to use within the local object to match against the target
* @param (NSString*)targetParameter the property within the class you wish to join with that will be matched with the left parameter.
* @return (DBQuery*) this value can be discarded or used to nest queries together to form clear and concise statements.
*/
- (DBQuery*)joinTo:(Class)joinClass leftParameter:(NSString*)leftParameter targetParameter:(NSString*)targetParameter;

下面是一个 swift 示例,链接两个表(员工和登录)。

Employee.query().joinTo(Login, leftParameter: "login", targetParameter: "Id")

显然,该类无法在运行时更改,因此您可以在 Dictionary 对象中找到结果,joinedResults

尽管您所描述的内容更多地与关系有关,但我建议您查看此处的一对多示例:

Relating two objects in DBAccess

因此,根据您的具体情况,您可以将以下方法添加到您的 Post 类中。

func media -> DBResultSet {
return Media.query().whereWithFormat("post_id = %@", withParameters: [self.post_id]).fetch()
}

这将从媒体表中查找并返回结果。

关于ios - 在 swift 中编写连接查询 DBAccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com