我想创建一个类似动态where子句的东西,但要为此努力。
框架: 我想使用4个Togglebuttons,它们显然以“ Togglebutton.ischecked()”返回一个为真或为假的布尔值。据此,我想选择数据库的正确课程。 切换按钮的值为“基本”,“通用”,“罕见”和“稀有”。 如果按下Togglebuttons,它将调用DB方法,然后使用数据库的数据来设置我的布局。
问题: 我在创建正确的查询时遇到问题,该查询会相应地更改whereArguments和whereClause。据我了解,whereClause需要一个字符串,而whereSelection需要一个StringArray。
我尝试过的事情:
例如如果ToggleButton(tb)tbBasic = true,tbUncommon = false,tbCommon = true,tbRare = true,则需要String []其中的Args = new String [] {“ Basic”,“ Common”,“ Rare”}不能使用类似“添加”方法的方法来相应地创建它。
例如如果我的DBHelper方法getAllHeros被调用,我每次都可以创建一个新的Arraylist,从而为我提供正确的whereArgs。
ArrayList<String> whereArgsList = new ArrayList<>();
if(btnBasic){
whereArgsList.add("Basic");
}
if(btnCommon){
whereArgsList.add("Common");
}
if(btnUncommon){
whereArgsList.add("Uncommon");
}
if(btnRare){
whereArgsList.add("Rare");
}
Cursor c = db.query("TableHeros", null,whereClause, whereArgsList.toArray(new String[0]),null,null,null,null);
与之相关的问题显然是whereClause也必须相应地更新。因此,在Arraylist大于一个Element的情况下,whereClause也将需要相同数量的元素。
我希望我的观点很明确,并且愿意尝试任何建议
答案 0 :(得分:1)
您尚未指定WHERE子句的外观。但是假设:
:-
public Cursor getMyRows(boolean btnBasic, boolean btnCommon, boolean btnUncommon, boolean btnRare) {
ArrayList<String> whereArgsList = new ArrayList<>();
String whereclause = null;
String[] whereargs = null;
if(btnBasic){
whereArgsList.add("Basic");
}
if(btnCommon){
whereArgsList.add("Common");
}
if(btnUncommon){
whereArgsList.add("Uncommon");
}
if(btnRare){
whereArgsList.add("Rare");
}
StringBuilder whereclauseToBe = new StringBuilder();
for(String s: whereArgsList) {
if (whereclauseToBe.length() > 1) {
whereclauseToBe.append(" OR ");
}
whereclauseToBe.append(COL_TASK_TITLE); //<<<<<<<<< change to your column
whereclauseToBe.append(" = ? ");
}
if (whereclauseToBe.length() < 1) {
Log.d("WHERE CLAUSE","No selections so WHERE CLAUSE and WHERE ARGS have been set as null");
} else {
whereclause = whereclauseToBe.toString();
Log.d("WHERE CLAUSE ", "The generated WHERE CLAUSE would be... WHERE " + whereclauseToBe.toString());
}
if (whereArgsList.size() > 0) {
whereargs = new String[whereArgsList.size()];
whereargs = whereArgsList.toArray(whereargs);
}
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.query(TABLE, null,whereclause, whereargs ,null,null,null,null); //<<<<<<<<<< Change TABLE to table name
return c; //<<<<<<<<<< Could just do `return db.query(TABLE.........`
}
以上内容通过(来自活动)进行了测试:-
db.getMyRows(false,false,false,false);
db.getMyRows(true,false,false,false);
db.getMyRows(true,false,true,false);
db.getMyRows(false,true,true,false);
db.getMyRows(false,false,false,true);
db.getMyRows(true,true,true,true);
写入日志的结果是:-
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: No selections so WHERE CLAUSE and WHERE ARGS have been set as null
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: The generated WHERE CLAUSE would be... WHERE title = ?
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: The generated WHERE CLAUSE would be... WHERE title = ? OR title = ?
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: The generated WHERE CLAUSE would be... WHERE title = ? OR title = ?
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: The generated WHERE CLAUSE would be... WHERE title = ?
10-22 04:44:23.975 1429-1429/? D/WHERE CLAUSE: The generated WHERE CLAUSE would be... WHERE title = ? OR title = ? OR title = ? OR title = ?