Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return a record based on only a partial match of that record?

I've done quite a bit of research on what I thought would be an easy question but I cannot find what I am looking for. I am simply trying to return a record as a match with only a search term matching part of the text in the record.

For example, if the user searches for "ed" I would like any record that contains "ed" to be returned. So if there was a record with the name "Edward" in the name column it would be returned as a match. If there was a record with "finished" in the description, it would also return that record as a match.

I have looked into full text search but not sure if this is something that I would need to do or if it would even do what I need.

As always, I'm not looking for an answer per say, I'm just looking for a direction.

like image 905
Droid Avatar asked Dec 16 '22 13:12

Droid


2 Answers

Never used SQLite before, but does it have the "LIKE" operator?

In MySQL, you can do something like:

SELECT name FROM list WHERE name LIKE '%ed%';
like image 135
Thomas Kelley Avatar answered Feb 09 '23 01:02

Thomas Kelley


Here is some code that will do what you want if you are querying a content provider. If you are querying a sqlite database object directly, the parameters are similar but different.

String search = "ed";

// surround it with the SQL wildcard '%'
String q = "%" + search + "%"

Cursor c = getContentResolver().query(uri,
    new String[] { column1, column2 },
    "column_name like ?",
    new String[] { q },
    null);

The snippet will search the content provider at uri for the string ed in column_name.

like image 38
Steve Prentice Avatar answered Feb 08 '23 23:02

Steve Prentice