Reputation: 67
I want to search in a sqlite database, but I have a problem. When I search in database and show results in listview , it show items which have n't my search word. For example I search for a word in the title column. But list view show some items from title which have n't this word. But maany column have this word. List view show words from title column which both row items have this words. Here is my code:
String sql = "SELECT * FROM " + TABLE + " WHERE " + TITLE +" OR "+ MAANY + " LIKE '%" + inputSearch.getText().toString() + "%'";
final SQLiteDatabase mydb = new MyDatabase(ArdicActivity.this).getWritableDatabase();
final Cursor c = mydb.rawQuery(sql, null);
List<String> array = new ArrayList<String>();
while(c.moveToNext()){
String uname = c.getString(c.getColumnIndex("nome"));
String maany = c.getString(c.getColumnIndex("conteudo"));
array.add(uname);
array.add(maany);
}
I hope to get my purpose.Excuse me for my bad English.
Upvotes: 0
Views: 177
Reputation: 164089
Your statement is wrong.
Change to this:
String pattern = "'%" + inputSearch.getText().toString() + "%'";
String sql = "SELECT * FROM " + TABLE + " WHERE " + TITLE + " LIKE " + pattern +
" OR "+ MAANY + " LIKE " + patern;
I don't know how your code compiled, but the right syntax is like this:
SELECT * FROM tablename WHERE column1 LIKE '%something%' OR column2 LIKE '%something%'
Upvotes: 1