user818569
user818569

Reputation: 19

Issue with rawquery SQL Android

I am building a contact app which is using SQL database. 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);
}

Upvotes: 0

Views: 461

Answers (3)

user818569
user818569

Reputation: 19

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

Upvotes: 1

oneriflebird
oneriflebird

Reputation: 11

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

Upvotes: 1

Sarwar Erfan
Sarwar Erfan

Reputation: 18068

From your code, I can see that you are trying to use regular expression with LIKE operator, which is not valid.

Upvotes: 0

Related Questions