-1

I am trying to implement a LIke query to get results from database according to user input in search box.

Below is the method I am using to get the results

public Cursor getSearchResults(String query) {
    // TODO Auto-generated method stub
    //Toast.makeText(myContext, "dhfhgjhfghdfjghxdf", Toast.LENGTH_LONG).show();

    String[] args={query};

    return(getReadableDatabase().rawQuery("SELECT _id,chapter FROM chapters  WHERE chapter LIKE '%query%'", args));


}

Here the %query% is coming from search action im my searchResultActivity.java

protected void onCreate(Bundle savedInstanceState) {


// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_query_results);

mQueryText = (ListView) findViewById(R.id.txt_query);
Toast.LENGTH_LONG).show();
 // Get the intent, verify the action and get the query
 Intent intent = getIntent();

 String query = intent.getStringExtra(SearchManager.QUERY);
 Toast.makeText(SearchResultsActivity.this,query, Toast.LENGTH_LONG).show();

  dbBookHelper = new BooklistHelper(this);
    ourCursor = dbBookHelper.getSearchResults(query);
    startManagingCursor(ourCursor);
    adapter = new AuthorAdapter1(ourCursor);
    mQueryText.setAdapter(adapter);

    //mQueryText.setOnItemClickListener(onListClick);

    }

But i am getting this error "cannot bind argument at index 1 because the index is out of range android. the statement has 0 argument".
Please help me in this. Let me know if you need more information. Thanks In advance..:)

Priya
  • 31
  • 6

2 Answers2

0

Try this:

String[] args= new String[]{query};
return(getReadableDatabase().rawQuery("SELECT _id,chapter FROM chapters  WHERE chapter LIKE '% ? %'", args));
Eefret
  • 4,344
  • 3
  • 27
  • 44
  • To declare an array that way in java you should do it like this `String[] args= new String[]{query};` you can see other ways of declaring arrays in here [http://stackoverflow.com/a/1200646/1259923] – Eefret Jul 15 '14 at 22:09
0

Your SQL doesn't have any ? variable placeholders but you have one bind argument query.

Change the SQL to

SELECT _id,chapter FROM chapters  WHERE chapter LIKE '%' || ? || '%'

where || is string catenation operator in SQL.

laalto
  • 137,703
  • 64
  • 254
  • 280
  • Hi, This code is working, can u please tell me how can i display NO RESULTS FOUND " on my SearchResultActivity Screen if the Query string dosen't matches with any of in my database. – Priya Jul 16 '14 at 08:09