0

I am calling a function in order to do a select statement in a bundled SQLite database. The function returns an array of structs. The database is being read correctly as I have put some print commands in the code. However the final array only has 1 row in it, which contains all the data, instead of 16 rows of structs.

The struct code, which is in databaseHelper.swift, is...

struct ButtonData: Hashable {
    let english: String
    let categoryID: Int
    let indonesian: String
}

The database code, in databaseHelper, is

class DatabaseHelper {

    var buttonVars = [ButtonData]()

    var database: Connection!

    let buttonsTable = Table("Button")
    let english = Expression<String>("english")
    let category = Expression<String>("category")
    let categoryID = Expression<Int>("ID")
    let filename = Expression<String>("filename")
    let indonesian = Expression<String>("indonesian")

    init() {

        do {
            let path = Bundle.main.path(forResource: "sga", ofType: "db")!
            let database = try Connection(path, readonly: true)
            self.database = database
            print("Database initialized at path \(path)")
        } catch {
            print("error")
        }
    }


    func queryDatabase(passedCategory: String) -> [ButtonData] {

        do {
            let buttons = try self.database.prepare(self.buttonsTable.filter(self.category==passedCategory))
            for row in buttons {
                print("English: \(row[self.english]), ID: \(row[self.categoryID]), Indonesian: \(row[self.indonesian])")
            //    buttonVars.append(ButtonData(english: row[english], categoryID: row[categoryID], indonesian: row[indonesian]))
                buttonVars.append(ButtonData(english: row[english], categoryID: row[categoryID], indonesian: row[indonesian]))
            }
        } catch {
            print(error)
        }
        print(buttonVars[0])
        print(buttonVars[1])
        print(buttonVars[2])
        print(buttonVars[3])
        print(buttonVars[4])
        print(buttonVars[5])
        print(buttonVars[6])
        print(buttonVars[7])
        print(buttonVars[8])
        print(buttonVars[9])
        print(buttonVars[10])
        print(buttonVars[11])
        print(buttonVars[12])
        print(buttonVars[13])
        print(buttonVars[14])
        print(buttonVars[15])
        return buttonVars
    }
}

The function code, which is in SoundPageView.swift (this page calls the database function), is...

    func getArrayValues() {
        let buttonRows = [DatabaseHelper().queryDatabase(passedCategory: category)]
        let btnCount: Int = buttonRows.count
        print(btnCount)
        print(buttonRows[0])
    }

The print values in the console show me that btnCount = 1 but before the array is returned, it is made of 16 rows. It is only after it is returned that it is reduced to 1 row.

enter image description here

Can anyone tell me what I'm doing wrong? I don't know how to access the data. Thanks.

1 Answer 1

1

I'm not sure why you are putting the brackets in this call:

let buttonRows = [DatabaseHelper().queryDatabase(passedCategory: category)]

The func queryDatabase returns an array on its own. I think it should be this:

let buttonRows = DatabaseHelper().queryDatabase(passedCategory: category)

Otherwise your result will be an array with one entry, which is the result of the call to queryDatabase.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was it, you saved me again Hal! Thanks again!

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.