dancingbush
dancingbush

Reputation: 2281

Search SQLite database returns empty cursor

I am using a searchView to get a query to search database with a string, the Cursor does not return as null but is empty.

The call to search from my search activity (which is a ListActivity implementing SearchView.OnQueryTextListener):

@Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        //handleIntent(intent);
        Log.d("LIST ACTIVITY SEARCH", "onQueryTextCahanged called: "+ newText);
        Cursor c = data.getWordMatches(newText, null);

        Log.d("LISTACTIVITY", "CURSOR QUERY ROWS/COLS = "+ c.getCount()+" "+c.getColumnCount());

        //now set bind cursor data to list view using custim ItemAdpter class
        adapter = new ItemAdapter(ViewListOfTests.this, c);
        this.setListAdapter(adapter);
        return false;
    }

And the search method in SQLite Databasetaking the String query from method above:

/*
     * methods to search database with String query form searchable widget intent 
    *  in ViewListOfDives ListActivity search bar. Each search returns a cursor which is set to adpter
    *  in ViewListOfYesys List Activity
    */

    public Cursor getWordMatches(String query, String[] columns){

        String selection = database.KEY_TESTNAME + " MATCH ?";
        String[] selectionArgs = new String[] {query+"*"}; //wildcard *

        return query(selection, selectionArgs, columns);


    }//end getWordMatches



    private Cursor query(String selection, String[] selectionArgs,
            String[] columns) {

        // return cursor form params passed getWordMatches(Styring query) from search widget ListActivity

        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(DATABASE_TABLE);

        dbHelperObject = new DbHelper(ourContext);

        Cursor cursor = builder.query(dbHelperObject.getReadableDatabase(), columns, selection, selectionArgs, null, null, null);

        if(cursor == null){
            return null;
        }else if(!cursor.moveToFirst()){
            cursor.close();
            return null;
        }

        return cursor;




    }//end query

The else if code executes here returning an empty cursor :

else if(!cursor.moveToFirst()){
                cursor.close();
                return null;

Any input appreciated! Ciaran

Upvotes: 0

Views: 1599

Answers (2)

dancingbush
dancingbush

Reputation: 2281

Got it, seems i was passing the wrong parameter to the curosr.rawQuery(). I was passing a ref to dataBase helper object getReadableDB call:

Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
            columns, selection, selectionArgs, null, null, null);

When I replaced this parameter with the database object it worked fine:

SQLiteDatabase ourDataBase = ourDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

Cursor cursor = builder.query(ourDataBase, columns, selection, selectionArgs, null, null, null);

The training documentation stated pass the former mDatabaseOpenHelper.getReadableDatabase() but this did not seem to work, not why.http://developer.android.com/training/search/search.html

Upvotes: 0

Hamid Reza
Hamid Reza

Reputation: 664

instead of this :

if(cursor == null){
    return null;
}else if(!cursor.moveToFirst()){
cursor.close();
   return null;
}

return cursor;


use this:

if(cursor == null){
    return null;
}else{
     cursor.moveToFirst();
}

return cursor;

Upvotes: 1

Related Questions