I am building a contact app which is using SQL databas开发者_StackOverflow社区e. I have a search function for user. User can search by key in an alphabet and then the list will show from the alphabet to z
. For example, if user key is t
, the list will show contact names starting from t
to z
. I create a table whose name is contactstable
and store contact name in sname
. Below is my code. However, it fails to search. Can anyone please help me?
public void search(View view) {
cursor = db.rawQuery("SELECT _id, sname FROM contactstable WHERE sname LIKE ? ORDER BY sname COLLATE NOCASE",
new String[]{"["+searchText.getText().toString() + "-z]%"});
adapter = new SimpleCursorAdapter(
this,
R.layout.employee_list_item,
cursor,
new String[] {"sname"},
new int[] {R.id.firstName});
setListAdapter(adapter);
}
I am not sure, it seems the SQLite cannot use
A Like B
You need to use the function
Like(B, A)
Please refer to the SQLite documentation
I am using the same method as the answer of below post and solve my problem.
How to use [charlist] Wildcard in android sql search
From your code, I can see that you are trying to use regular expression with LIKE
operator, which is not valid.
精彩评论