简体   繁体   English

Anorm 使用 LIKE 子句过滤查询

[英]Anorm filter a query with LIKE clause

Using scala play anorm and mysql, I try to make a sql query, filtering using a LIKE operator:使用 scala play anorm 和 mysql,我尝试进行 sql 查询,使用 LIKE 运算符进行过滤:

import java.sql.Connection
import anorm._

Class.forName("com.mysql.jdbc.Driver").newInstance()
implicit val conn = java.sql.DriverManager.getConnection(dbUrl)
val list = SQL"select firstName from person where lastName like '%tra%'".as(parser.*)

Exception in thread "main" java.lang.RuntimeException: No parameter value for placeholder: 1

here it should filter all persons whose lastName contains the 'tra' string.在这里它应该过滤所有姓氏包含“tra”字符串的人。 why is it not working?为什么它不起作用? is anorm (or java dbc) treating '%' as a special character? anorm(或java dbc)是否将“%”视为特殊字符? if so, how to escape this?如果是这样,如何逃避?

If you expected WHERE clause is something like '%pattern%' you will have to prepare string before passing it as argument.如果您期望 WHERE 子句类似于 '%pattern%' ,则必须在将其作为参数传递之前准备字符串。

SQL"""SELECT firstName FROM person WHERE lastName LIKE ${"%"+tra+"%"}""".
as(userParser.*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM