sqlite中的group_concat()返回undefined

时间:2013-05-13 14:02:09

标签: android sqlite cordova

sqlite manager返回结果中的相同查询是我如何在sqlite manager database creation中创建数据库,在sqlite manager insertion中创建数据库并在sqlite-manager group_concat on status col中检索值phonegap-android它返回我undefined.i需要结果像第3快照。我在phonegap-android中使用循环插入记录。 这是插入代码:

function insertInUserPrayerTable() {


                        db.transaction(function(tx)
                         {for(var i = 0;i<checkedValueofCheckbox.length;i++)
                        {checkedValueofCheckboxInsertArray.push('INSERT INTO user_prayers'

                             + '(prayer_id,user_id,date,status)'
                                + ' VALUES("'
                                + checkedValueofCheckbox[i]
                                + '",'
                                + window.localStorage
                                        .getItem("userid") + ',"'
                                + $('#datepicker').val() + '",'
                                + statusofNamaz[i] +');');

                console.log("array values:"+checkedValueofCheckboxInsertArray);
                        }
                         },errorCB,runInsertQuery());

                         }
function runInsertQuery() {
                        db
                                .transaction(
                                        function(tx) {
                                            for (var i = 0; i < checkedValueofCheckboxInsertArray.length; i++) {
                                                tx
                                                        .executeSql(
                                                                checkedValueofCheckboxInsertArray[i],
                                                                [],
                                                                successCBrunInsertQuery,
                                                                errorCBrunInsertQuery);
                                            }
                                        },errorCB,successCB);

                    }

我知道我做错了什么?

$('#showbtn')
.off('click')
.on('click', function() {
    db.transaction(function(tx) {
        tx.executeSql("SELECT date,group_concat(status)"
            + " FROM user_prayers WHERE user_id ='"+window.localStorage.getItem("userid")+"'"
            + " GROUP BY date",
            [], successCBofselect, errorCBofselect);
    }, errorCB, successCB);
});

function successCBofselect(tx,results) {
    var htmlstring = "";
    if (results != null && results.rows != null && results.rows.length > 0) {
        console.log(results);<!-- [object SQLResultSet] -->
        console.log(results.rows);<!-- [object SQLResultSetRowList] -->
        for (var i = 0; i < results.rows.length; i++) {
            alert(""+results.rows.length);<!-- 8-->
            alert(""+results.rows.item(i)); <!-- [object object]-->
            deleteDataTemp[i] = results.rows.item(i).status;
            alert(""+deleteDataTemp[i]);<!-- undefined-->
            htmlstring += '</div>';
        }
        $("#data").empty().append(htmlstring).trigger('create');
    }
});

其中“checkedValueofCheckbox [i]”是一个包含值sof的数组,所有选中和未选中的复选框和“statusofNamaz [i]”包含一个状态数组“1”或“0” 插入console.log 05-14 16:21:16.335:I / Web Console(8505):数组值: INSERT INTO user_prayers(prayer_id,user_id,date,status)VALUES(“FAJR”,1,“2013-05-15”,1); INSERT INTO user_prayers(prayer_id,user_id,date,status)VALUES(“ZOHAR”,1,“2013-05-15”,1); INSERT INTO user_prayers(prayer_id,user_id,date,status)VALUES(“ASR”,1,“2013-05-15”,0); INSERT INTO user_prayers(prayer_id,user_id,date,status)VALUES(“MAGHRIB”,1,“2013-05-15”,0); INSERT INTO user_prayers(prayer_id,user_id,date,status)VALUES(“ISHA”,1,“2013-05-15”,0);在file:///android_asset/www/js/PrayersManagerjs/namaz.js:144

需要结果1,1,0,0,0只有按日期分组的状态

1 个答案:

答案 0 :(得分:1)

tx.executeSql("SELECT date,group_concat(status) FROM user_prayers  WHERE user_id ='"+ 
    window.localStorage.getItem("userid")+"' GROUP BY date" ...

如果没有与WHERE子句匹配的行,group_concat(status)将返回null。我猜你的表中没有包含与用户ID匹配的行。你是说window.localStorage.getItem("user_id")吗?如果您将该值输出到控制台,它是您所期望的吗?

此外,通常不要使用字符串连接来构建SQL语句。请改用参数绑定,例如:

tx.executeSql("SELECT date,group_concat(status) FROM user_prayers WHERE user_id ='?' GROUP BY date",
    [window.localStorage.getItem("userid")] ...

它更安全,在我看来也更具可读性。