简体   繁体   中英

Select random row from Safaris client-side database

I'm experimenting with the built in SQL support in the Safari Browser and I want to select a random query via Javascript.

SELECT * FROM questions ORDER BY random()

Returns not authorized to use function: random

See this screenshot .

Any suggestions?

Query using a non-random 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 be something like this: https://stackoverflow.com/a/962890/490560

Perhaps something like the following would work:

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

Share and enjoy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM