mysql – 如何在没有使用Anorm选择结果时执行代码?
作者:互联网
当存在与WHERE子句匹配的记录时,此代码可以正常工作:
val pinfo = SQL("SELECT * FROM tableName WHERE id={id}").on("id" -> "scala")
pinfo().map { row =>
println("have something")// runs when selected
}
什么都没有被选中会发生什么?
当没有从MySQL中选择记录时,我想打印以下内容.
println("nothing is selected")//if no row comes
解决方法:
SQL(…)()返回一个Stream [SqlRow],流有isEmpty
方法:
val pinfo: Stream[SqlRow] = SQL("SELECT * FROM tableName WHERE id={id}").on("id" -> "scala")()
if(!pinfo.isEmpty) pinfo.map { row => println("have something") }
else println("nothing is selected")
也来自REPL:
scala> 1 #:: 2 #:: empty
res0: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> res0.isEmpty
res1: Boolean = false
scala> empty
res2: scala.collection.immutable.Stream[Nothing] = Stream()
scala> res2.isEmpty
res3: Boolean = true
标签:mysql,scala,playframework,playframework-2-2,anorm 来源: https://codeday.me/bug/20190612/1224852.html