Select random row from Safaris client-side database

白昼怎懂夜的黑 提交于 2020-01-11 09:45:43

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/3500842/select-random-row-from-safaris-client-side-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!