Cannot convert from bool to FMResult set for SQL query results?

180 Views Asked by At

I am utilizing FMDB as a way to integrate a SQLite database into my XCode project. I have been following this tutorial on the matter and it has been excellent, for the most part. Creating a table and inserting objects into the table has been painless. However, I run into a bit of an issue when try to save queried results from db to a variable in app. Here is the code I use for this desired process (including the struct for the variable's type):

struct pulledMessage{
    var userIDs: String
    var userNames: String
    var message: String
    var sender: String
    var messageNumber: Int
}
func loadMessageData() -> [pulledMessage]!{
        var messagesPulled: [pulledMessage]!

        if openDatabase(){//correctly opens the database
            let query = "select * from messages order by messageNumber asc"
            do{
                print(database)
                let results: FMResultSet = try database.executeStatements(query)
                //^statement above causes error
                //The type FMResultSet is not usable for some reason
                while results.next(){
                    let message = pulledMessage(userIDs: String(results.string(forColumn: "userIDs")), userNames: String(results.string(forColumn: "userNames")), message: String(results.string(forColumn: "message")), sender: String(results.string(forColumn: "sender")), messageNumber: Int(results.string(forColumn: "messageNumber")))
                    if messagesPulled == nil{
                        messagesPulled = [pulledMessage]()
                    }
                    messagesPulled.append(contentsOf: message)
                }

            }
            catch{
                print(error.localizedDescription)
            }
            database.close()
        }
        return messagesPulled


    }

Now the issue that I am currently running into isn't necessarily logic-flow oriented. I can understand the process and what the commands are trying to do. The issue is rather with the line let results = try database.executeStatements(query). This line is a near direct copy of the one found on the aforementioned database, though it is the cause of the error in the results.next() line and the let messages = pulledMessage(...). The .executeStatements means that results has the type of bool. .next() cannot be called on type bool and I also cannot do results.string or whatever. While I understand the issue, I do not know how to amend it. Since .executeStatements will return a bool, and not the actual result of the select * from messages SQL command, how can I set results to whatever type/object that the tutorial seems to have intended?

Edit: I would like to set the results of the query as a FMResultSet so that I can use .next() and assign my variable to some of the result's properties. However, the method for which I have tried to do that (viewable in the code I have posted) is setting: let results: FMResultSet = try database.executeStatments(). This, though, gives me an error that I cannot convert a bool to an FMResultSet, so I am stuck?

1

There are 1 best solutions below

2
On BEST ANSWER
func loadMessageData() -> [pulledMessage]!{
        var messagesPulled: [pulledMessage]!

        if openDatabase(){
            let query = "select * from messages order by messageNumber asc"
            do{
                print(database)
                let results:FMResultSet = try database.executeQuery(query, values: nil)

                while results.next(){
                    let message = pulledMessage(userIDs: String(results.string(forColumn: "userIDs")), userNames: String(results.string(forColumn: "userNames")), message: String(results.string(forColumn: "message")), sender: String(results.string(forColumn: "sender")), messageNumber: Int(results.string(forColumn: "messageNumber")))
                    if messagesPulled == nil{
                        messagesPulled = [pulledMessage]()
                    }
                    messagesPulled.append(contentsOf: message)
                }

            }
            catch{
                print(error.localizedDescription)
            }
            database.close()
        }
        return messagesPulled


    }

Try with this