我想这SQL
查询工作:ORDER BY RANDOM()在科尔多瓦/ PhoneGap的
"SELECT * FROM people WHERE gender='female' ORDER BY val, RANDOM();"
但是,我得到了以下错误:
E/SQLiteLog: (1) not authorized to use function: random
是否有相当于办法做这个?如果可能的话只用一个查询。
我读到Cordova/Phonegap - SQLite禁用RANDOM()
函数,因为它花费了大量内存,并创建了临时表,但该表最多有800行,结果被WHERE
过滤。
编辑: 我曾尝试使用下面的函数 弄乱的结果,但后来我得到一个错误!
function shuffle(array) {
var counter = array.length,
temp, index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
var resultArray = [];
for (var x = 0; x < results.rows.length; x += 1) {
resultArray.push(results.rows.item(x));
}
var res = shuffle(resultArray);
for (var i = 0; i < res.rows.length; i++) {
name = (res.rows.item(i).name);
}
错误:
Uncaught TypeError: Cannot read property 'length' of undefined
谢谢!
如果你有那么几行,为什么不把它们随机放入内存中? –
如何通过“val”首先进行排序,然后将它们随机存储在内存中? – candlejack
@JoachimIsaksson我添加了更多的代码,请检查它! – candlejack