Java – SQLite selecting from like statements does not work?
•
Java
When I look at the database and run this query, I get the expected results
SELECT * FROM users WHERE options LIKE '%[-15,-3]%';
However, when I use the prepared statement shown below, UUID is null
String opt = "[-15,-3]"; //example
PreparedStatement ps = sqlite.connection.prepareStatement(
"SELECT * FROM users WHERE options LIKE '%" + opt + "%'"
);
ResultSet rs = ps.executeQuery();
String uuid = null;
while (rs.next()){
uuid = rs.getString("member");
}
rs.close();
ps.close();
if(uuid != null){
System.out.println("not null: " + uuid);
return Database.getUser(UUID.fromString(uuid);
}
For the above code, nothing is returned in the result set This is strange because I used the same query in the SQLite viewer and returned the correct rows How can I solve this problem? I don't see the problem
UPDATE
When I directly use "select * from factors where claim like '% [– 15, - 3]%' instead of variables in the prepared statement, it works normally Why can't I use string variables? I checked the variable and it prints well to the console
Solution
After a lot of repeated experiments, I solved this problem. Should I always use it? And set the string
PreparedStatement ps = sqlite.connection.prepareStatement(
"SELECT * FROM users WHERE options LIKE ?"
);
ps.setString(1,"%" + opt + "%");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
