简体   繁体   中英

Android How to add search suggestions using “like ?” SQLite query

I am implementing a data search into my application, with the ability to automatically display custom search suggestions, that will be shown below the search box, when the user types in a partial word string into a search box. Users would then be able to click on one of the suggestions, which will run another activity for the selected search item. I am using a standard searchview widget as part of the Action Bar.

The requirement is to populate the search suggestions using the standard SQLite query with the " LIKE ('%" + search_string + "%')" clause. I have studied the Android Developers search instructions, including the excellent Searchable Dictionary source code example. However, all available examples and instructions show how to extract the data from the text file, using the Content provider and then using the SQLite FTS3 extensions, to run the "Match ?" (word) type query. As mentioned above, this type of query is not suitable, as it does not allow for searching of the partial strings anywhere inside the searched data set.

I would be interested in a simple code example how to achieve the above search requirements. Any suggestions or links to the example source code would be highly appreciated.

You can implement the "LIKE" query as below;

  dbh = new DbHelper(this); SQLiteDatabase db = dbh.getWritableDatabase(); Cursor c = db.query("TableName", new String[]{"ColumnName"} , "ColumnName LIKE ?" ,new String[]{"%"+ filter+ "%" }, null, null, null); while(c.moveToNext()) { // your calculation goes here } 

Hope this will guide you.

please use this query: SELECT * FROM ConditionType where NAME like '%s%'

this query is use in android using sqlite database

String[]  Args= {"%"+selectionArgs[0]+"%"}; 
    Cursor c = qb.query(database, projection, selection, Args,
            null, null, sortOrder);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM