webkit中的sqlite是否支持RANDOM()函数?

时间:2011-04-07 22:50:47

标签: sqlite random safari cordova

我正在为iOS编写一个基于Phonegap的应用程序,我正试图通过Javascript从我的Safari webkit数据库中返回10个随机行。我的疑问是:

queryString = "SELECT * FROM SBA_TABLE
 WHERE (cat_gastrointestinal = 1)
 AND (answered_correctly = 0 OR answered_correctly = 1 OR answered_correctly = 2)
 ORDER BY RANDOM() LIMIT 10";

tx.executeSql(queryString, [], querySuccess5, errorCB);

如果我省略'ORDER BY RANDOM()'语句,这会让我相信这不受支持,这是有效的。有没有我可以使用的RANDOM方法,还是我必须生成10个随机数并进行10次数据库调用?!谢谢,尼克

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,我解决了这个问题:

seed = (Math.random() + 1) * 1111111;
queryString = "SELECT * FROM SBA_TABLE
 WHERE (cat_gastrointestinal = 1)
 AND (answered_correctly = 0 OR answered_correctly = 1 OR answered_correctly = 2)
 ORDER BY ROWID * ? % 10000 LIMIT 10";

tx.executeSql(queryString, [ seed ], querySuccess5, errorCB);

在查询中使用Javascript Math.random()函数和表ROWID上的模数。

答案 1 :(得分:0)

暂定的解决方法,以防ORDER BY RANDOM()具体在该版本上无效:

"SELECT *, RANDOM() AS rnd FROM SBA_TABLE
WHERE (cat_gastrointestinal = 1)
AND (answered_correctly = 0 OR answered_correctly = 1 OR answered_correctly = 2)
ORDER BY rnd LIMIT 10";