我与iOS10应用程序中的数据库有一个工作连接,使用SQLite.swift。
我希望为特定的大学选择详细信息,在那里我可以从另一个视图控制器传入一个IHE_ID。
我只想为那所特定的大学选择行,但我无法让查询工作。但是,我可以使用准备循环遍历所有数据,然后从中选择我需要的数据,这当然比我需要的资源更密集,因为我已经从发送视图控制器以anIHE Int的形式传入了特定的anIHE。
连接起作用了所以省略了密码..。
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: Public\n")
} else {
buildIHE.append("Sector: Private\n")
}
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")
}最后,如果能够使查询工作,我将使用所选的元素。我想我可能只是查询的语法出了问题,但是任何帮助都是值得赞赏的。
"if let query“的行在Xcode中有此错误:条件绑定的初始化程序必须具有可选类型,而不是'Table‘。这使我认为这是我使用.select语句时所做的一些事情,而且在一般情况下使用SQLite.swift和with都是新的。
最后一件事是anIHE作为Int进入这个函数,而IHE_ID是表达式,如下面的代码所示。我想这可能是问题的一部分。
发布于 2016-12-12 07:59:41
Initializer for conditional binding must have Optional type错误意味着if let v = expr语句右侧的表达式不是可选的:使用if是没有意义的,Swift编译器说您应该只编写let v = expr。
实际上,IHEs.select(...).filter(...)返回Table类型的非可选值。
这不是您所期望的数据库行,因为查询已经定义,但尚未执行。毕竟,您没有使用db:从哪里加载行?
解决方案是恢复数据库连接,并加载一行。这是用拔毛方法完成的。
if let ihe = try db.pluck(IHEs.select(...).filter(...)) {
print("Name \(ihe[IHE_Name])")
}https://stackoverflow.com/questions/41081801
复制相似问题