Mehrdad Salimi
Mehrdad Salimi

Reputation: 1426

How use "LIKE" and "%" to check similarity to variable in android(Java code)

I am working on Android app using Java, I need to query database in Java code to check if userinput(variable) statement is contain words which are in my SQLite DataBase using LIKE in query and rowQuery method in java code,

I used this code but it did not work:

cursor = db.rawQuery("SELECT shompet FROM sentence WHERE " + column + " LIKE '%"
                              + newMessage + "%'", null);

newMessage is my variable(userInput)

I read similar topics but either they are not my answer or they are so complicated.

Upvotes: 0

Views: 556

Answers (1)

CL.
CL.

Reputation: 180192

In the expression 'hi' LIKE '%hi there%', it is not possible to find any characters to replace the % wildcards so that the strings would match.

You need to do the comparison the other way around, i.e., 'hi there' LIKE '%hi%':

db.rawQuery("SELECT shompet FROM sentence" +
            " WHERE ? LIKE '%' || " + column + " || '%'",
            new String[] { newMessage });

Upvotes: 2

Related Questions