ORDER BY RANDOM()在科尔多瓦/ PhoneGap的 [英] ORDER BY RANDOM() in Cordova/Phonegap

查看:238
本文介绍了ORDER BY RANDOM()在科尔多瓦/ PhoneGap的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个 SQL 查询工作:

 选择*从不同人的性别='女'ORDER BY VAL,RANDOM();

不过,我得到了以下错误:


  

E / SQLiteLog:(1)未经授权使用的功能:随意


有一个等效的方式做到这一点?如果可能的话用一个查询。

我读到科尔多瓦/ PhoneGap的 - SQLite的禁用 RANDOM()函数,因为它花费了大量的内存,并创建临时表,但表将有800行最大,结果是由过滤WHERE

修改
我曾尝试使用下面的函数弄乱结果
但后来我得到一个错误!

 函数洗牌(阵列){
    VAR计数器= array.length,
        温度,指数;
    //虽然有数组中的元素
    而(计数器大于0){
        //选择一个随机指数
        指数= Math.floor(的Math.random()*计数器);
        //减少1计数器
        计数器 - ;
        //并与它交换的最后一个元素
        TEMP =阵列[窗口];
        数组[计数器] =阵列[指数]
        数组[索引] =温度;
    }
    返回数组;
}
变种resultArray = [];
为(变量X = 0; X&下; results.rows.length; X + = 1){
    resultArray.push(results.rows.item(X));
}
VAR解析度=洗牌(resultArray);
对于(VAR I = 0; I< res.rows.length;我++){
    名称=(res.rows.item(I)。名称);
}

错误:


  

遗漏的类型错误:无法读取的未定义的属性长度


谢谢!


解决方案

  VAR解析度=洗牌(resultArray);
对于(VAR I = 0; I< res.length;我++)
{
    名称=(RES [I]。名称);
}

I'm trying this SQL query to work:

"SELECT * FROM people WHERE gender='female' ORDER BY val, RANDOM();"

But, I got the following error:

E/SQLiteLog: (1) not authorized to use function: random

Is there an equivalent way to do this? If possible with a single query.

I read that Cordova / Phonegap - SQLite disables RANDOM () function because it spends a lot of memory, and create temporary tables, but the table will have 800 rows maximum, and the results are filtered by WHERE.

EDIT: I have tried to mess up the result using the following function But then I get an error!

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

ERROR:

Uncaught TypeError: Cannot read property 'length' of undefined

Thanks!

解决方案

var res = shuffle(resultArray);
for (var i=0; i < res.length; i++) 
{
    name = (res[i].name);
}

这篇关于ORDER BY RANDOM()在科尔多瓦/ PhoneGap的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆