Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite select from LIKE statement not working?

Tags:

java

sqlite

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

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

However when I use a prepared statement as seen below, the 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 code above, nothing is returned in the result set. Which is very strange because I used the same query with an SQLite viewer and it returned the proper rows. How can I solve this? I don't see an issue.

UPDATE

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

like image 898
ThatGuy343 Avatar asked Feb 01 '15 09:02

ThatGuy343


1 Answers

I solved it after a lot of trial and error, turns out I should've been using a ? and set the string.

PreparedStatement ps = SQLite.connection.prepareStatement(
        "SELECT * FROM users WHERE options LIKE ?"
);

ps.setString(1, "%" + opt + "%");
like image 199
ThatGuy343 Avatar answered Sep 28 '22 07:09

ThatGuy343