0

I've been working with Sqlite.Swift for quite a while now and I'm stuck. I'm trying to get an Int out of the .count of only the amount of rows containing the String value "1" in the column "yes or no".

I've been using the following function to randomly pick a row only once (without repeating). I thought this must be a good start to define the .count for only the described rows but I have absolutely no clue if this'd be possible.

This is how I got my "row randomizer" working:

func randomNumber() -> Int {

   var randomNumber = Int(arc4random_uniform(UInt32(try! database.scalar(table.select(ItemId.distinct.count)))))

   while pickedNumber == randomNumber {

          randomNumber = Int(arc4random_uniform(UInt32(try! database.scalar(table.select(ItemId.distinct.count)))))

     }

      pickedNumber = randomNumber

      return randomNumber

  }

let randomRow = randomNumber()

thanks!

Aeger
  • 138
  • 2
  • 11

1 Answers1

3

Answering my own question:

Simply this did the job:

let count = try! database.scalar(tableName.where(expression == "1").count)

Edit:

You can see the ! here. I did this because I'm sure there is a table where there's a column of that name with cells containing a String value of 1. If you want to go a more secure way use the do / try / catch mechanic.

Aeger
  • 138
  • 2
  • 11