0

I'm trying to do a check in my database, I want to make sure that there is only one entry (row) in my database and only use that row and must not add any additional entry (row).

if self.heroTable.count >= 1 {
    let setHeroLevelandExpi = self.heroTable.insert(self.level <- 1, self.expi <- 25, self.maxExpi <- 30)
}

But it says that "Binary operator '>=' cannot be applied to operands of type 'ScalarQuery<Int>' and 'Int'". Am I missing anything?

2
  • What is heroTable? What SQLite library are you using? But why both checking the count? Perform an "INSERT OR REPLACE" query instead of a simple "INSERT". Of course that requires a unique key. Commented Mar 16, 2018 at 23:07
  • I'm using this layer of SQLite (github.com/stephencelis/SQLite.swift) if it helps. the heroTable was created via let heroTable = Table("hero") is this information correct for your question? Commented Mar 16, 2018 at 23:17

1 Answer 1

1

Looking at the library, you actually need to run the count query.

if let rowCount = try? db.scalar(heroTable.count) as? Int64, rowCount >= 1 {
    // there are 1 or more rows in the table
}
Sign up to request clarification or add additional context in comments.

2 Comments

This one works to me, just checking some changes, can I ask how do you get individual values from this database like level and put it to variable?
That's a whole different question but I'd start by reading the documentation for the library you are using and the sample shown on the project site.

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.