SQLiteDatabase错误:“in1”附近:语法错误

时间:2016-10-07 07:21:38

标签: java android sqlite android-sqlite

我点击了第一个按钮。这个时间显示错误in1

我在查询中将=放在“=”附近的一些错误:语法错误

如何解决?

在这个查询中,我使用String Builder连接到多个字符串

 public List<People> getPeople(String category_id) {
    List<People> peoples = new ArrayList<>();
    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        StringBuilder sb = new StringBuilder();
        sb.append(",");
        Cursor cursor = db.rawQuery("select * from people where category_id in " +(category_id + sb + category_id) , null);

        while (cursor.moveToNext()) {
            String peopleName = cursor.getString(cursor.getColumnIndex(PEOPLE_NAME));
            String peopleImage = cursor.getString(cursor.getColumnIndex(PEOPLE_IMAGE));

            People people = new People();
            people.setPeopleName(peopleName);
            people.setPeopleImage(peopleImage);

            peoples.add(people);
        }

    } catch (Exception e) {
        Log.d("DB", e.getMessage());
    }
    return peoples;
}

1 个答案:

答案 0 :(得分:4)

您的语法必须是:

"select * from people where category_id in (" + category_id + sb + category_id + ")"

括号是IN子句的一部分。

或简单地说:

"select * from people where category_id in (" + category_id + ", " + category_id + ")"

因为stringbuilder不是必需的。