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

2019-06-13 05:32发布

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条回答
成全新的幸福
2楼-- · 2019-06-13 06:15

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 });
查看更多
登录 后发表回答