Whenever the search query contains the ‘ char my android app crashes, because it thinks the ‘ splits the String.
Here’s an example –
Cursor selCur= myDataBase.rawQuery("SELECT * FROM drinks WHERE name like '%"+editTextSearch.getText().toString()+"%'", null);
Whenever editTextSearch.getText().toString() contains a ‘ (for example – “Ben’s”) the app crashes.
I can’t think of anyway around it.
Could really use help.
Thanks!
What you really should do is use the
SQLiteDatabasemethodrawQuerywith a String array for selection args:Cursor selCur= myDataBase.rawQuery("SELECT * FROM drinks WHERE name like ?", new String[] {"%" + editTextSearch.getText().toString() + "%"});You place a question mark
?in place of the string you wish to have there, and put the actual string in an array, like i have above. You can have multiple?in your query string, just make sure your array size matches how many?you have.Edit: also for using
like, you may want to add the%symbol inside the String array like i have just changed. I found that this results in a query that i expect rather than including the%in the query string itself.