如何使查询动态

时间:2016-01-26 20:51:24

标签: java android

您好试着让查询动态化。 应用程序菜单列表中的每个按钮都有一个按钮ID,我无法控制按钮ID,因为数据库是从远程服务器填充的。

我可以获取按钮ID,但如何将其传递给查询?

public ArrayList<HashMap<String, String>> getAllDailys() {
    ArrayList<HashMap<String, String>> locationsList;
    locationsList = new ArrayList<HashMap<String, String>>();
    String ButtonId = intent.getStringExtra("buttonId");
    String selectQuery = "SELECT * FROM locations WHERE id_button =  ?", ButtonId;
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("id", cursor.getString(0));
            map.put("locations", cursor.getString(2));
            map.put("checks", cursor.getString(3));
            locationsList.add(map);
        } while (cursor.moveToNext());
    }
    database.close();
    return locationsList;
}

1 个答案:

答案 0 :(得分:-1)

改变这个:

String selectQuery = "SELECT * FROM locations WHERE id_button =  ?", ButtonId;

要:

String selectQuery = "SELECT * FROM locations WHERE id_button = \'" + ButtonId + "\'";