1

我在. _ 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))
}
4

1 回答 1

2

这取决于你的mapResult功能。恐怕会mapResult尝试tag_id从 ResultSet 值中获取。在这种情况下,您的 SQL 查询仅返回countminmax

于 2018-04-16T09:35:58.527 回答