在SQLITE中查询“COALESCE”

时间:2013-05-09 06:40:11

标签: android sqlite cordova

这是PhoneGap android的表格:

  

tx.executeSql(                 “CREATE TABLE IF NOT NOT EXISTS user_prayers('id'INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,'prayer_id'TEXT,'user_id'   INTEGER,'date'DATETIME,'status'INTEGER)“,[],successCB,errorCB);

并使用循环插入行:

function insertInUserPrayerTable() {
                        db.transaction(function(tx)
                         {for(var i = 0;i<5;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,successCB);
            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() {
                                //getRecord();
                                        db
                                                .transaction(
                                                        function(tx) {
                                                            tx.executeSql("SELECT a.date, COALESCE(a.status,'') AS 'fajr', COALESCE((SELECT b.status FROM user_prayers b WHERE b.date = a.date AND b.id != a.id LIMIT 1),'') AS 'zohar' FROM user_prayers a WHERE user_id='"+window.localStorage.getItem("userid")+"' GROUP BY a.date",[],successCBofselect,errorCBofselect);},errorCB,successCB);

但它没有返回我的状态,而相同的查询在sql小提琴中正常工作:

1 个答案:

答案 0 :(得分:1)

答案来自sqlite: is there a way to make a conditional OR / COALESCE in the SELECT clause?

SELECT CASE WHEN (TitleSP = '' OR TitleSP IS NULL)
            THEN COALESCE(TitleEN, '')
        WHEN (TitleEN = '' OR TitleEN IS NULL)
            THEN TitleSP
        ELSE (TitleSP || ' / ' || TitleEN)
   END AS Title, 

...