SqLite在一个查询中搜索五个单词

时间:2017-08-09 21:01:37

标签: android sqlite

我需要在一列中一次搜索五个单词。列中必须存在所有五个单词。使用我的代码,只搜索第一个单词。我知道我需要遍历“listOfWords”并将循环传递给查询。谁会有一个如何做到这一点的例子?我的尝试:

        String query = "SELECT 1 FROM sowpods WHERE field1 IN " + listOfWords;
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        for (int i = 0; i < listOfWords.length(); i++) {
            do {
                Toast.makeText(this, "Word exists!", Toast.LENGTH_SHORT).show();

            } while (cursor.moveToNext());
        }
    } else {
        Toast.makeText(this, "Word does not exist", Toast.LENGTH_SHORT).show();
        cursor.close();
        db.close();
    }
    cursor.close();
    db.close();
}

更新 目前,唯一对我有用的是:

        String query = "SELECT 1 FROM sowpods WHERE field1 LIKE 'dojfdgkhdg'";
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        Toast.makeText(this, "Yay!", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "No, don't exist!", Toast.LENGTH_SHORT).show();
    }

    String query2 = "SELECT 1 FROM sowpods WHERE field1 LIKE 'cat'";
    Cursor cursor2 = db.rawQuery(query2, null);
    if (cursor2.moveToFirst()) {
        Toast.makeText(this, "Yay!", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "No, don't exist!", Toast.LENGTH_SHORT).show();
    }

    String query3 = "SELECT 1 FROM sowpods WHERE field1 LIKE 'dog'";
    Cursor cursor3 = db.rawQuery(query3, null);
    if (cursor3.moveToFirst()) {
        Toast.makeText(this, "Yay!", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "No, don't exist!", Toast.LENGTH_SHORT).show();
    }

然而,它看起来不正确,因为1.它需要大量重复的代码,2。我不确定拥有多个游标是非常有效的资源使用。 我会非常感激一些指针。干杯!

新更新

这就是我现在所处的位置:

Cursor cursor = db.query(true, "sowpods", new String[]{"field1"}, "field1 IN (?)", new String[]{" 'dog', 'cat', 'horse', 'cow'"}, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                cursor.moveToNext();
                Toast.makeText(this, "Word 1 exists", Toast.LENGTH_SHORT).show();
                cursor.moveToNext();
                Toast.makeText(this, "Word 2 exists", Toast.LENGTH_SHORT).show();
                cursor.moveToNext();
                Toast.makeText(this, "Word 3 exists", Toast.LENGTH_SHORT).show();
                cursor.moveToNext();
                Toast.makeText(this, "Word 4 exists", Toast.LENGTH_SHORT).show();
            } while (cursor.moveToNext());
        }
        return cursor;
    }

这不会返回任何内容,所以欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

如果要搜索包含列表中任何作品的列,请使用此查询

SELECT 1FROM sowpods 
   WHERE field1 LIKE '%word1%'
   OR field1 LIKE '%word2%'
   OR field1 LIKE '%word3%'

如果要搜索包含所有单词的列

SELECT 1FROM sowpods 
   WHERE field1 LIKE '%word1%'
   AND field1 LIKE '%word2%'
   AND field1 LIKE '%word3%'

如果你想搜索你的列字符串包含任何工作作为子字符串和特定的,那么使用这个

SELECT 1 FROM sowpods 
  WHERE CHARINDEX('word1', field1 ) > 0
  OR CHARINDEX('word2', field1 ) > 0
  OR CHARINDEX('word3', field1 ) > 0

如果你想搜索你的列字符串包含所有工作作为子字符串和特定的,那么使用这个

 SELECT 1 FROM sowpods 
  WHERE CHARINDEX('word1', field1 ) > 0
  AND CHARINDEX('word2', field1 ) > 0
  AND CHARINDEX('word3', field1 ) > 0