How use “LIKE” and “%” to check similarity to vari

2019-06-13 06:03发布

问题:

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.

回答1:

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 });