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

发布于 2024-10-31 05:58:53 字数 470 浏览 1 评论 0原文

我正在为 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 次数据库调用?!谢谢,尼克

I'm coding a Phonegap based app for iOS, and am trying to return 10 random rows from my Safari webkit database via Javascript. My query is:

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);

This works if I omit the 'ORDER BY RANDOM()' statement which leads me to believe this is not supported. Is there a RANDOM method I could use, or do I have to generate 10 random numbers and make 10 database calls?! Thanks, Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-11-07 05:58:53

我刚刚遇到了同样的问题,我用这样的解决方法解决了它:

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 的模数。

I just had the same problem, I solved it with such workaround:

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);

Using the Javascript Math.random() function and a modulo on the table ROWID in the query.

若无相欠,怎会相见 2024-11-07 05:58:53

暂定解决方法,以防 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";

Tentative workaround, in case ORDER BY RANDOM() specifically isn't functional on that version:

"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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文