问题
I have a table like the following image
I need to get all the English words that its Kurdish Word contains "بةرز",
So i cant use
select English from Table1 where Kurdish like '%بةرز%';
because it also accepts the words that are sub-string in another word like these ،يبلبةرز ، سيس بةرز
,
And when i try to use Regular Expression in my query:
query = "SELECT English FROM Table1 WHERE Kurdish REGEXP '^[.، ]*("+"بةرز" +")[ ،.]*$'";
s.execute(query);
it shows the following Exception
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.1 data type of expression is not boolean
Is the problem is with my regular expression or what?
Note that i'am using UCanAccess for my database connection
回答1:
Instead of
columnName REGEXP 'pattern'
I believe you need to use
REGEXP_MATCHES(columnName, 'pattern')
This seems to work for me ...
String targetString = "بةرز";
try (PreparedStatement ps = conn.prepareStatement(
"SELECT English FROM Table1 " +
"WHERE Kurdish = ? " +
"OR REGEXP_MATCHES(Kurdish, ?) " +
"OR REGEXP_MATCHES(Kurdish, ?) " +
"OR REGEXP_MATCHES(Kurdish, ?) ")) {
ps.setString(1, targetString);
ps.setString(2, "^" + targetString + "[.، ]+.*");
ps.setString(3, ".*[.، ]+" + targetString + "$");
ps.setString(4, ".*[.، ]+" + targetString + "[.، ]+.*");
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
... although there might be a more elegant way of doing it.
来源:https://stackoverflow.com/questions/33282360/ucanaccesssqlexception-ucaexc3-0-1-data-type-of-expression-is-not-boolean