.prepare vs. .select



我使用sqlite.swift。

我想选择一所特定大学的详细信息,其中我从另一个视图控制器传递了一个IHE_ID。

我只想为那个特定的大学选择该行,但是我无法找到工作的疑问。但是,我可以用准备循环循环所有数据,然后从中选择我需要的数据,这当然比我需要的更密集的资源,因为我已经从发送视图控制器中传递了特定的IHE_ID作为anihe int。<<<<<<<<<<<<<<<<<<<<

连接正在省略该代码...

 do {
                        let db = try Connection(destinationDBPath, readonly: true)
                        let IHEs = Table("IHE")
                        let IHE_ID = Expression<Int>("IHE_ID")
                        let IHE_Name = Expression<String>("IHE_Name")
                        let IHE_COE_Url = Expression<String>("IHE_COE_URL")
                        let IHE_Sector = Expression<Int>("IHE_Sector")
                        let IHE_COE_Name = Expression<String>("IHE_COE_Name")
        for ihe in try db.prepare(IHEs){
                        if (ihe[IHE_ID] == anIHE){
                            // build this string, otherwise ignore rest of dataset (doing this because query isn't working)
                            buildIHE = "Name: (ihe[IHE_Name])n"
                            buildIHE.append("URL: (ihe[IHE_COE_Url])n")
                            // buildIHE.append("Sector: (ihe[IHE_Sector])n")
                            if (ihe[IHE_Sector] == 0) {
                                buildIHE.append("Sector: Publicn")
                            } else {
                                buildIHE.append("Sector: Privaten")
                            }
                            buildIHE.append("College of Education Name: (ihe[IHE_COE_Name])n")
                        }
                    }
                    print ("Got through building IHE portion of view")

我想做的就是使用它而不是for循环。

    if let query = IHEs.select(IHE_ID,IHE_Name,IHE_COE_Url,IHE_Sector,IHE_COE_Name).filter(IHE_ID == anIHE){
                print("Query successful for (anIHE) with name (query[IHE_Name])")
            // more actions to build the string I need would then occur
            } else {
                print("Query has failed or returned nil")
            }

最后,如果可以使查询工作,我将使用选定的元素。我想我可能在查询上的语法有问题,但是对任何帮助都表示赞赏。

带有"如果让查询"的行中有此错误:有条件绑定的初始化器必须具有可选类型,而不是"表"。这使我认为这是我使用.select语句的东西,而只是使用sqlite.swift和Swift。

最后一件事是,Anihe以INT的形式进入此功能,而IHE_ID则如本代码所示。我认为这可能是问题的一部分。

Initializer for conditional binding must have Optional type错误意味着if let v = expr语句右侧的表达式不是可选的:没有必要使用,而Swift Compiler表示您应该只编写let v = expr

的确,IHEs.select(...).filter(...)返回类型Table的非访问值。

这不是您期望的数据库行,因为查询已定义为,但还没有执行。毕竟,您不使用db:从哪里加载行?

解决方案是带回数据库连接,并加载一行。这是通过拔出方法完成的。

if let ihe = try db.pluck(IHEs.select(...).filter(...)) {
    print("Name (ihe[IHE_Name])")
}

最新更新