如何使用Swift中的查询语句检索数据?

How to retrieve the data using query statement in Swift?

我想从名为 cache_image 的 table 中检索数据,但我不知道该怎么做,而且我在这里遇到错误。

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 的 table 和名为 image_namedownload_url 的两列,但是当我尝试使用

访问它时
print(value.string(forColumn: "IMAGE_NAME")

它正在显示警告:

Couldn't find image_name and for column "download_url", it is showing "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)