如何使用 Slick 3 从 SQL select 查询中获取结果
How to get result from SQL select query with Slick 3
我想 运行 向我的数据库发送自定义 sql select 请求。在光滑的 3 docs 之后,我得到了这样的结果
"com.typesafe.slick" %% "slick" % "3.0.1",
"mysql" % "mysql-connector-java" % "5.1.35",
import slick.driver.MySQLDriver.api._
val db = Database.forURL(url, username, password, driver = driver)
val s = sql"""select name, email from users""".as[(String, String)]
val f: Future[Unit] = db.run(DBIO.seq(s))
但我想获得一个元组序列。我怎样才能得到它?
来自DBIO
documentation:
def seq[E <: Effect](actions: DBIOAction[_, NoStream, E]*):
DBIOAction[Unit, NoStream, E]
A simpler version of sequence that takes a number of DBIOActions with
any return type as varargs and returns a DBIOAction that performs the
individual actions in sequence (using andThen), returning () in the
end.
因此,DBIO.seq
将始终 return Unit
。如果您只想执行单个查询,只需将查询本身传递给 db.run
方法即可。因此,你的最后一行是:
val f: Future[Seq[(String, String)] = db.run(s)
我想 运行 向我的数据库发送自定义 sql select 请求。在光滑的 3 docs 之后,我得到了这样的结果
"com.typesafe.slick" %% "slick" % "3.0.1",
"mysql" % "mysql-connector-java" % "5.1.35",
import slick.driver.MySQLDriver.api._
val db = Database.forURL(url, username, password, driver = driver)
val s = sql"""select name, email from users""".as[(String, String)]
val f: Future[Unit] = db.run(DBIO.seq(s))
但我想获得一个元组序列。我怎样才能得到它?
来自DBIO
documentation:
def seq[E <: Effect](actions: DBIOAction[_, NoStream, E]*): DBIOAction[Unit, NoStream, E]
A simpler version of sequence that takes a number of DBIOActions with any return type as varargs and returns a DBIOAction that performs the individual actions in sequence (using andThen), returning () in the end.
因此,DBIO.seq
将始终 return Unit
。如果您只想执行单个查询,只需将查询本身传递给 db.run
方法即可。因此,你的最后一行是:
val f: Future[Seq[(String, String)] = db.run(s)