我在. _ ScalikeJdbc
在尝试运行以下代码时
val dbTableSQLSyntax: SQLSyntax = SQLSyntax.createUnsafely(dbTableName)
sql"""
SELECT
COUNT(*) AS count,
MIN($distributionColumn) AS min,
MAX($distributionColumn) AS max
FROM
$dbTableSQLSyntax
""".stripMargin.
map(mapResult).
single().
apply().
get()
我收到这个错误
scalikejdbc.ResultSetExtractorException: Failed to retrieve value because For input string: "tab_id". If you're using SQLInterpolation, you may mistake u.id for u.resultName.id.
at scalikejdbc.WrappedResultSet.wrapIfError(WrappedResultSet.scala:27)
at scalikejdbc.WrappedResultSet.get(WrappedResultSet.scala:479)
at scalikejdbc.WrappedResultSet.longOpt(WrappedResultSet.scala:233)
...
- 如何在不使用Query-DSL的情况下摆脱这个错误?
- 在上面的代码片段中有什么我可以改进的(在性能/安全方面)吗?
框架/库
Scala 2.11.11
"org.scalikejdbc" %% "scalikejdbc" % "3.2.0"
编辑-1
针对@Kazuhiro Sera的回答,我提供了我的mapResult
方法
def mapResult(rs: WrappedResultSet): (Long, Long, Long) = {
val count: Long = rs.long("count")
val minOpt: Option[Long] = rs.longOpt("min")
val maxOpt: Option[Long] = rs.longOpt("max")
(count, minOpt.getOrElse(0), maxOpt.getOrElse(Long.MaxValue))
}