0

我想知道在哪里可以找到这样的代码引发的异常:

def readFromDB: String = {
    db_sqlite_xml.withSession {
      implicit db: Session =>
        xmlQuery.first.text

    }
  }

我在光滑的 scaladoc ( http://slick.typesafe.com/doc/2.0.1/api/#package )中找不到它;我在 javadoc 的 tableQuery 类中搜索了方法“first”,但没有任何成功。

谢谢。

奥利维尔

ps:这是我的答案,它正在工作:

def readFromDB: String = {
    db_sqlite_xml.withSession {
      implicit db: Session =>
        xmlQuery.firstOption.map(u=>u.text).getOrElse("")
    }
  }
}

感谢您的回答,它帮助了我。

4

1 回答 1

1

该方法属于UnitInvoker特征,来自scaladoc

final def first()(implicit session: SessionDef): R

Execute the statement and return the first row of the result set. 
If the result set is empty, a NoSuchElementException is thrown.

如果我能给你一个建议,而不是尝试捕获异常,你应该使用firstOption

final def firstOption()(implicit session: SessionDef): Option[R]

Execute the statement and return the first row of the result set wrapped in Some, 
or None if the result set is empty.

通过这种方式,您可以对查询结果进行参数匹配,如下所示:

def readFromDB: String = {
  db_sqlite_xml.withSession {
    implicit db: Session =>
      xmlQuery.firstOption match {
        case Some(value) => value.text
        case _ => // handle no result
      }
  }
}
于 2014-04-19T17:48:40.373 回答