Select a random row from the Safaris client database

I am experimenting with native SQL support in Safari Browser and I want to select a random query through Javascript.

SELECT * FROM questions ORDER BY random()

Return not allowed to use function: random

See this screenshot .

Any suggestions?

+3
source share
2 answers

Query using a nonrandom order, then shuffle the results:

tx.executeSql('SELECT * FROM questions',[], function(tx, resultSet) {
    var resultArray = [];

    for(var i=0; i < resultSet.rows.length; i+=1) {
        resultArray.push(resultSet.rows.item(i));
    }

    var shuffledArray = shuffle(resultArray);

    // do something with the shuffled array...
});

Where shuffle()could there be something like this: fooobar.com/questions/12404 / ...

+3
source

Perhaps something like the following:

SELECT *
  FROM (SELECT RANDOM() as RANDOM_NUM, Q.*
          FROM QUESTIONS Q)
  ORDER BY RANDOM_NUM

Share and enjoy.

0
source

Source: https://habr.com/ru/post/1760036/


All Articles