查询的RESULTSET的隐式转换 [英] implicit conversion of RESULTSET for queries
问题描述
我正在使用Scala 2.10,并且在Slick上遇到问题(普通查询,java.sql.ResultSet).如果我写
这样的查询 Q.query[String, ResultSet](query).list(rs.getString("id"))
日食会告诉我could not find implicit value for parameter rconv: scala.slick.jdbc.GetResult[java.sql.ResultSet]
此问题的最重要来源( http://slick.typesafe. com/doc/0.11.2/sql.html )没有帮助.如何编写这些隐式转换?还有其他其他熟悉的方式来表示Slick中的ResultSet
吗?
让我尝试阐明一下:).如果有ResultSet
,则应具有实际将结果集映射到的类型.例如.将保留您的行的元组或案例类.如果是自定义(案例)类,则必须提供一个implicit
GetResult
来描述如何从jdbc ResultSet
映射到您的类. .list
的参数应该是您希望Slick放在准备好的语句的占位符中的值,而不是您得到的ResultSet
的值(假设这就是rs
的意思).>
建议的用法如下:
import scala.slick.jdbc.{GetResult, StaticQuery}
import StaticQuery.interpolation
val price = 1000.0
// use tuples
val expensiveDevices: List[Device] =
Q.query[String, (Long,Double,Date)]("select * from DEVICE where PRICE > ?").list( price )
// or a case class (needs implicit GetResult for Device)
case class Device(id: Long,price: Double,acquisition: Date)
implicit val getDeviceResult =
GetResult(r => Device(r.<<, r.<<, r.<<))
val expensiveDevices2: List[Device] =
Q.query[String, Device]("select * from DEVICE where PRICE > ?").list( price )
// or the even nicer interpolation syntax
val expensiveDevices3: List[Device] =
sql"select * from DEVICE where PRICE > $price" .as[Device].list
I'm using Scala 2.10 and have problems with Slick (plain queries, java.sql.ResultSet). If I write queries like
Q.query[String, ResultSet](query).list(rs.getString("id"))
eclipse will tell me could not find implicit value for parameter rconv: scala.slick.jdbc.GetResult[java.sql.ResultSet]
My most important source for this issue ( http://slick.typesafe.com/doc/0.11.2/sql.html ) does not help. How do I write these implicit conversions? And is there any other, familar way of representing ResultSet
s in Slick?
Let's me try to shed some light :). Where you have ResultSet
, you should have a type you actually map result sets to. E.g. a tuple or a case class that will hold your row. In case of a custom (case) class you will have to provide an implicit
GetResult
that describes how to map from a jdbc ResultSet
to your class. The argument to .list
should be a value that you want Slick to put in the placeholder of your prepared statement, not something that you get our of a ResultSet
(assuming that that is what rs
stands for).
The suggested use is something like this:
import scala.slick.jdbc.{GetResult, StaticQuery}
import StaticQuery.interpolation
val price = 1000.0
// use tuples
val expensiveDevices: List[Device] =
Q.query[String, (Long,Double,Date)]("select * from DEVICE where PRICE > ?").list( price )
// or a case class (needs implicit GetResult for Device)
case class Device(id: Long,price: Double,acquisition: Date)
implicit val getDeviceResult =
GetResult(r => Device(r.<<, r.<<, r.<<))
val expensiveDevices2: List[Device] =
Q.query[String, Device]("select * from DEVICE where PRICE > ?").list( price )
// or the even nicer interpolation syntax
val expensiveDevices3: List[Device] =
sql"select * from DEVICE where PRICE > $price" .as[Device].list
这篇关于查询的RESULTSET的隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!