0

Hye..I managed to create search function to search using multiple keywords. The problem is the keywords must be exist inside my database column that i had set. If i search using 3 keywords which is 2 keywords match inside database but 1 more keyword is not exist. The result was displayed nothing.

How could I ignore the non existing keywords?? just take the keywords that exists inside my database only. Means, if the user enter many keywords, system will read only keywords that match inside my database only and it will ignore the keywords that doesn't match. Below is my code.

String s = txtLelaki.getText().toString();
                String s = txtLelaki.getText().toString();
                String[] words = s.split(" ");
                String sql = null;

                String where = helper.KEY_LELAKI + " = ?";
                String relation = " or ";
                String wildcard1 = "'%";
                String wildcard2 = "%'";

                StringBuilder build = null;
                for(int i=0;i<words.length; i++)
                {
                    words[i] = new StringBuilder(wildcard1).append(words[i]).append(wildcard2).toString();
                    if(i==0)
                        build = new StringBuilder(where);
                    else
                        build.append(relation).append(where);                       
                }

                Cursor c = database.query(helper.DB_TABLE_LELAKI, null, build.toString(), words, null, null, null);


                if(c!= null)
                {
                    c.moveToFirst();
                    int soalanColumn = c.getColumnIndex(helper.MASALAH_LELAKI);
                    int getId = c.getColumnIndex(helper.ID_LELAKI);
                    if(c.isFirst())
                    {
                        do
                        {
                            idList.add(c.getLong(getId));
                            results.add(c.getString(soalanColumn));


                        }while(c.moveToNext());
                    }

                }

                adapter.updateList(results);
                listView.setTextFilterEnabled(true);

            }//end try

                catch(SQLException e)
                {
                    Log.v("caer", e.toString());    
                }
4

1 回答 1

0

重写
我仍然不明白你想要做什么,但是看看你之前的问题:android search with multiple keywords,我假设你想要一个这样的查询:SELECT * FROM Table WHERE foo like ? OR foo like ? ...对于每个可能的关键字。您需要自己构建此WHERE子句:

String where = helper.KEY_LELAKI + " = ?";
String relation = " OR ";
String wildcard = "%";

StringBuilder builder;
for(int i = 0; i < words.length; i++) {
    // Surround the keyword with "%", i.e. "%cat%"
    words[i] = new StringBuilder(wildcard).append(words[i]).append(wildcard).toString();

    // Build the WHERE clause
    if(i == 0)
        builder = new StringBuilder(where);
    else
        builder.append(relation).append(where);
}

Cursor c = myDataBase.query(helper.DB_TABLE_LELAKI, null, 
        builder.toString(), words, null, null, null);

如果s在您的示例中,"cat mustache pull"您的查询将包含包含关键字短语“cat”、“mustache”或“pull”的每一行。

于 2013-03-01T17:13:26.893 回答