联盟与WHERE子句 - 安卓的Eclipse [英] UNION with WHERE clause - Android Eclipse
问题描述
我要创建使用UNION用于搜索多个表的搜索界面。我的问题是,在最后的表中的数据是不断出现的,当我试图搜索单词的唯一的事。如何搜索的表正常,其中的条件是,这个名字应该根据其首字母进行搜索。
下面是我的code:
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
诠释计数){
// TODO自动生成方法存根
如果(。searchText.getText()的toString()长度()大于0){
光标= db.rawQuery(选择_id AS _id,名作为名称,说明AS降序从comm_acc其中名称是怎样的?+
UNION SELECT _id AS _id,名作为名称,说明AS降序从comm_filems其中名称是怎样的?+
UNION SELECT _id AS _id,名作为名称,说明AS降序从comm_psu其中名称是怎样的?+
UNION SELECT _id AS _id,名作为名称,说明AS降序从comm_shells其中名称是怎样的?+
UNION SELECT _id AS _id,名作为名称,说明AS降序从comm_sic其中名称是怎样的?+
UNION SELECT _id AS _id,名作为名称,说明AS降序从comm_stp其中名称是怎样的?,
新的String [] {searchText.getText()的toString()+%});
适配器=新SimpleCursorAdapter(
getActivity(),
R.layout.search_command_list_item,
光标,
新的String [] {姓名,说明},
新INT [] {R.id.commandName,R.id.commandDesc},0);
setListAdapter(适配器);
}
其他 {
}
}
});
}
未使用的参数得到NULL值;只有 comm_acc
表实际进行搜索。
您必须给六个参数的查询:
字符串是searchPattern = searchText.getText()的toString()+%。
光标= db.rawQuery(SELECT ...?......?......?......?......?......?......
新的String [] {是searchPattern,是searchPattern,是searchPattern,
是searchPattern,是searchPattern,是searchPattern});
或使用相同的参数都喜欢:
光标= db.rawQuery(SELECT ...名字怎么样?1+
UNION ALL SELECT ...名字怎么样?1+
...
UNION ALL SELECT ...名字怎么样?1,
新的String [] {searchText.getText()的toString()+%});
I'm creating a search interface using UNION for searching multiple tables. My problem is, the data in the last table is the only thing that keeps appearing when I try to search for the word. How can I search the tables properly where the condition is, the name should be searched according to its first letter.
Here is my code:
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (searchText.getText().toString().length() > 0) {
cursor = db.rawQuery("SELECT _id AS _id, name AS name, desc AS desc FROM comm_acc WHERE name LIKE ?" +
" UNION SELECT _id AS _id, name AS name, desc AS desc FROM comm_filems WHERE name LIKE ? " +
"UNION SELECT _id AS _id, name AS name, desc AS desc FROM comm_psu WHERE name LIKE ? " +
"UNION SELECT _id AS _id, name AS name, desc AS desc FROM comm_shells WHERE name LIKE ? " +
"UNION SELECT _id AS _id, name AS name, desc AS desc FROM comm_sic WHERE name LIKE ? " +
"UNION SELECT _id AS _id,name AS name, desc AS desc FROM comm_stp WHERE name LIKE ?",
new String[]{searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(
getActivity(),
R.layout.search_command_list_item,
cursor,
new String[] {"name", "desc"},
new int[] {R.id.commandName, R.id.commandDesc}, 0);
setListAdapter(adapter);
}
else {
}
}
});
}
Unused parameters get a value of NULL; only the comm_acc
table is actually searched.
You have to give six parameters to the query:
String searchPattern = searchText.getText().toString() + "%";
cursor = db.rawQuery("SELECT ... ? ... ? ... ? ... ? ... ? ... ? ...",
new String[]{ searchPattern, searchPattern, searchPattern,
searchPattern, searchPattern, searchPattern });
or use the same parameter in all LIKEs:
cursor = db.rawQuery(" SELECT ... name LIKE ?1 " +
"UNION ALL SELECT ... name LIKE ?1 " +
...
"UNION ALL SELECT ... name LIKE ?1",
new String[]{ searchText.getText().toString() + "%" });
这篇关于联盟与WHERE子句 - 安卓的Eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!