使用等效的IN(a,b)

时间:2017-10-04 13:41:42

标签: javascript select indexeddb

我正在尝试从websql转移到indexeddb但似乎无法复制SELECT查询: “SELECT * FROM tableA WHERE cid ='”+ cid +“'AND hid IN(”+ hid +“,1)ORDER BY hid DESC LIMIT 1”;

function getMyData(e) {
    var s ="";
    var hid = "value1";
    var cid = "value2";
    var transaction = db.transaction(["table"], "readonly");
    var store = transaction.objectStore("table");
    var index = store.index("myIndex"); //  myIndex was created onupgradeneeded from ['hid','cid']
    var request = index.openCursor(IDBKeyRange.only([hid, cid]));
    request.onsuccess = function(e){
        var cursor = e.target.result;
        if(cursor){
            s +="<h2>Key "+cursor.key+"</h2><p>";
                for (var field in cursor.value){
                    s += field+"="+cursor.value[field]+"<br>";
                }
            s +="</p>"
            cursor.continue();
        }
            document.querySelector("#mySpan").innerHTML = s;
}
}

如果hid = value1和cid = value2,我想显示数据,但如果商店索引不包含[value1,value2],我想显示商店索引[aDefaultValue,value2]中的数据。 (sql语句中的默认值为1)。 我尝试了大写,但这会返回索引[value1(-1),value2]数据,而不是[1,value2]数据。

1 个答案:

答案 0 :(得分:0)

最简单的可能是:

var r1 = index.get([aDefaultValue, cid]);
var r2 = index.get([hid, cid]);
// Since requests are processed in order, r1 guaranteed to succeed
// before r2 succeeds
r2.onsuccess = function() {
  var result = (r2.result !== undefined) ? r2.result : r1.result;
};