我想从名为cache_image的表中检索数据,但是我不确定该怎么做,并且在这里遇到了错误。

func checkCachedImages() ->Bool{
    database?.open()
    var count = Int()
    for i in images{
        do{
            if let value = database?.executeQuery("SELECT DOWNLOAD_URL FROM CACHED_IMAGE WHERE IMAGE_NAME = ?", withArgumentsIn: [i]){
                while value.next(){
                    print(value.string(forColumn: "IMAGE_NAME"))
                    print(value.string(forColumn: "DOWNLOAD_URL"))
                    count += 1
                    print(count)
                }
            }
        }catch {
            print(error)
        }
    }
    database?.close()
    if count == images.count{
        print("The count is \(count)")
        return true
    }

    return false
}

我确实有一个名为cached_image的表和两个名为image_namedownload_url的列,但是当我尝试使用
print(value.string(forColumn: "IMAGE_NAME")

它显示警告:

找不到图片名称,列“download_url”的列显示为“Optional(”“)”

请帮我解决这个问题。

最佳答案

您不能从结果中读取image_name,因为它不是SELECT子句的一部分,但是您不需要它,因为您可以从i中获取它

var numberOfRows = 0
var urls: [String] = ()
print("Image name: \(i)")
if let result = try database?.executeQuery("SELECT download_url FROM CACHED_IMAGE WHERE IMAGE_NAME = ?", values: [i]) {
    while result.next() {
        numberOfRows += 1
        let url = result.stringForColumnIndex(0)
        urls.append(url)
        print("\(url), \(i)")
    }
}
print(numberOfRows)

09-25 16:09