10

我正在尝试在我的网站上设置搜索功能,我正在尝试查找 IndexedDB 代码

SELECT "column" FROM "table" WHERE "column" LIKE "%keyword%"

我在IndexedDB 模糊搜索中找到了解决方案

db.transaction(['table'], 'readonly')
    .objectStore('table')
    .openCursor(IDBKeyRange.bound(keyword, keyword + '\uffff'), 'prev')
    .onsuccess = function (e) {
        e || (e = event);
        var cursor = e.target.result;
        if (cursor) {
            console.log(cursor.value.column);
            cursor.continue();
        }
    };

但我怎样才能找到“%keyword%”而不是“%keyword”?

4

1 回答 1

4

IndexedDB 中没有与 SQL WHERE 类似的东西。

我将遍历表/对象存储值并查看当前游标列是否包含关键字:

var keyword = "foo";
var transaction = db.transaction("table", "readwrite");
var objectStore = transaction.objectStore("table");
var request = objectStore.openCursor();
request.onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
        if (cursor.value.column.indexOf(keyword) !== -1) {                
            console.log("We found a row with value: " + JSON.stringify(cursor.value));
        }  

        cursor.continue();          
    }
};
于 2013-04-29T08:44:02.400 回答