简体   繁体   English

如何从Play2中的select语句中检索选项[List [X]]而不是List [X]!Scala Anorm?

[英]How to retrieve an Option[List[X]] instead of List[X] from a select statement in Play2!Scala Anorm?

In my Play2 app, I am trying to retrieve a list of users from one of my database table. 在我的Play2应用程序中,我试图从我的一个数据库表中检索用户列表。 The query responsible for this may potentially be empty if there is no row in the database matching the criteria (which is the firstName in our case). 如果数据库中没有符合条件的行(在我们的例子中是firstName),则负责此操作的查询可能可能为空。 That is why I have managed to implement it like this : 这就是为什么我设法像这样实现它:

    DB.withConnection { implicit connection =>
      SQL("""select u.* from users u
                where u.firstName like '%{firstName}%'
          """).on("firstName" -> firstName).as(userParser *)
    }

this query returns a List[User] but how can I return an Option[List[User]] since this query may not retrieve data corresponding to the provided param (firstName) ? 此查询返回List [User],但如何返回Option [List [User]],因为此查询可能无法检索与提供的param(firstName)对应的数据?

any help would be appreciated thanks... 任何帮助将不胜感激...

You don't need to. 你不需要。 If no user is found. 如果找不到用户。 The list will simply be empty. 该列表将是空的。

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

相关问题 播放Scala:如何使用anorm和Magic访问多个数据库[T] - Play Scala: How to access multiple databases with anorm and Magic[T] 从OS X上的iTunes获取曲目和文件名的列表(或播放它们的方法) - Get list of tracks and filenames (or a way to play them) from iTunes on OS X 从数据库选择列表中获取选项值 - Getting option value from database select list Web2py:当x和y是变量时,使用DAL编写SQL查询:“ SELECT x FROM y”,并将结果转换为列表? - Web2py: Write SQL query: “SELECT x FROM y” using DAL, when x and y are variables, and convert the results to a list? 从SQL Select语句获取列列表 - Getting List of Columns from a SQL Select statement 在Scala中,如何将主动脉结果列表从两字段数据表转换为Tuple2 - How to convert an anorm resultlist from a two-field data table to a Tuple2, in Scala 如何从firebase检索特定的数据列表? - How to retrieve specific list of data from firebase? 如何遍历SQL中select语句返回的行列表? - How can I iterate through a list of rows returned from a select statement in SQL? 如何使用select语句过滤链接列表中的内容? - How to filter contents in linked list using select statement? Play 2.0 Complex join查询如何解析(Anorm) - Play 2.0 Complex join query how to parse (Anorm )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM