1

I am trying to get a random row from a SQL SELECT query:

    SQL = 'SELECT * FROM tblQuestion'
    cursor = Databaseconnector.SELECT(SQL)
    rows = cursor.fetchall()

    rand = random.random(0,10)
    pprint.pprint(rows)

    row = rows(rand)

However, it complains that:

    Type Error: List object is not callable

on the final line

0

1 Answer 1

3

Use this:

row = random.choice(rows)

which will select a random element from the list rows.

Or check whether you can select directly a random row:

SELECT * FROM tblQuestion ORDER BY RAND() LIMIT 1
Sign up to request clarification or add additional context in comments.

Comments

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.