SQLite select from LIKE statement not working?

When I browse the database and run this query, I get the results as expected.

SELECT * FROM users WHERE options LIKE '%[-15,-3]%'; 

However, when I use a prepared statement as 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 very strange because I used the same query with SQLite viewer and returned the correct rows. How can i solve this? I do not see a problem.

UPDATE

When I directly use "SELECT * FROM factions WHERE claims LIKE '%[-15,-3]%'" in the prepared expression instead of a variable, it works fine. Why can't I use a string variable? I checked the variable and it prints fine to the console.

+5
source share
1 answer

I solved it after a lot of trial and error, it turned out that I should have used ? and set the line.

 PreparedStatement ps = SQLite.connection.prepareStatement( "SELECT * FROM users WHERE options LIKE ?" ); ps.setString(1, "%" + opt + "%"); 
+7
source

Source: https://habr.com/ru/post/1212364/


All Articles