1

I want to retrieve the data from my table named cache_image, but I am not sure how to do that and I am facing an error here.

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
}

I do have a table named cached_image and two columns named image_name and download_url but when i try to access that using

print(value.string(forColumn: "IMAGE_NAME")

it is displaying warning :

Couldn't find image_name and for column "download_url", it is showing "Optional("")"

Please help me to solve this issue.

7
  • why do you use the "DOWNLOAD_URL" and "IMAGE_NAME" in uppercase Commented Sep 18, 2018 at 9:27
  • I don't know because in query statement it was uppercased, so i used so Commented Sep 18, 2018 at 9:30
  • can you try to change the table name and columns name to lowercase Commented Sep 18, 2018 at 9:31
  • Yeah tried it but still the same @QuocNguyen Commented Sep 18, 2018 at 9:35
  • in the example of fmdb, they wrote try db.executeUpdate("INSERT INTO foo (bar) VALUES (?)", values: [1])", did you double check your querry type, like (?), not ?, withArgumentsIn and values Commented Sep 18, 2018 at 9:40

1 Answer 1

2

You can't read image_name from the result since it isn't part of the SELECT clause but you don't need it since you can get it from 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)
Sign up to request clarification or add additional context in comments.

10 Comments

Hello @Joakim Danielson I tried the above solution , but the print(result.intForColumnIndex(0)) produces 0
I don't know where you did that print
I want to print download_url, how to get that?
I do that, it is in the url variable. just modify my print statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.