在 SQLite 中动态使用 IN()

Using IN() dynamically in SQLite

我有以下查询:

public List<ListCoursesFilterModel> getListSubject(List<String> lstLang) {
    StringBuilder langs = new StringBuilder();
    StringBuilder questionMark = new StringBuilder();
    for (int i = 0; i < lstLang.size(); i++) {
        langs.append(",").append(lstLang.get(i).toLowerCase());
        questionMark.append(",?");
    }
    String res = langs.substring(1, langs.length());
    String qs = questionMark.substring(1, questionMark.length());
    List<ListCoursesFilterModel> list = new ArrayList<>();
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    Cursor cursor = db.query(
            false,
            "ShortMajorTBL",
            null,
            "langId IN(" + qs + ") AND identifier =?",
            new String[]{res, "S300"},
            null,
            null,
            null,
            null
    );
    try {
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    ListCoursesFilterModel listCoursesFilterModel = new ListCoursesFilterModel();
                    listCoursesFilterModel.setSubject(cursor.getString(cursor.getColumnIndex("subject")));
                    listCoursesFilterModel.setLangId(cursor.getString(cursor.getColumnIndex("langId")));
                    listCoursesFilterModel.setIsSelected(cursor.getString(cursor.getColumnIndex("isSelected")));
                    listCoursesFilterModel.setIdentifier(cursor.getString(cursor.getColumnIndex("identifier")));
                    listCoursesFilterModel.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                    listCoursesFilterModel.setCount(cursor.getInt(cursor.getColumnIndex("count")));
                    list.add(listCoursesFilterModel);
                } while (cursor.moveToNext());
            }
            return list;
        }
    } catch (Exception ex) {
        Throwable t = new Throwable(ex).fillInStackTrace();
        FirebaseCrash.report(t);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

lstLang 当我有一个值时 - 例如 en - 它有效并得到结果,但是当我有多个值时 - 例如 en,fr,ar - 它不显示任何结果。

IN 子句中的每个 值都是一个字符串。
因此,它必须被单独转义。

即:'en', 'fr', 'ar'.

所以,有类似的东西

...
qs = `'en', 'fr', 'ar'`; // somehow buid this dynamically
...
"langId IN(" + qs + ") AND identifier =?",
...