Barry Allen
Barry Allen

Reputation: 85

Python Sqlite: How would you randomly select a value-specific row?

I want to be able to randomly select rows that have a certain value. For example, I have a student table in sqlite that stores different characteristics (i.e Gender). I want to randomly pick a student that is male using python. I have looked at other questions (e.g Select a random row from the table using Python) but isn't relevant to value specific rows. How would I do this?

Upvotes: 2

Views: 1036

Answers (1)

Adobe
Adobe

Reputation: 13487

No python specific syntax is needed: sqlite has a random() function:

select
  *
from users

where gender == 'M'

order by random()

limit 1

For performance see this: https://stackoverflow.com/a/24591688/788700

Upvotes: 5

Related Questions