Sql 如何使用Scala Slick访问一行中的数据库条目

Sql 如何使用Scala Slick访问一行中的数据库条目,sql,scala,slick,Sql,Scala,Slick,我正在使用以下教程中的代码: sysgears.com/articles/building rest service with scala/#16 我的数据库架构: /** * Customer entity. * * @param id unique id * @param firstName first name * @param lastName last name * @param accountBala

我正在使用以下教程中的代码: sysgears.com/articles/building rest service with scala/#16

我的数据库架构:

    /**
     * Customer entity.
     *
     * @param id        unique id
     * @param firstName first name
     * @param lastName  last name
     * @param accountBalance account balance
     * @param birthday  date of birth
     */
     case class Customer(id: Option[Long], firstName: String, lastName: String, accountBalance: Int, birthday: Option[java.util.Date])

     /**
      * Mapped customers table object.
      */
     object Customers extends Table[Customer]("customers") {

       def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

       def firstName = column[String]("first_name")

       def lastName = column[String]("last_name")

       def accountBalance = column[Int]("account_balance")

       def birthday = column[java.util.Date]("birthday", O.Nullable)

       def * = id.? ~ firstName ~ lastName ~ accountBalance ~ birthday.? <>(Customer, Customer.unapply _)

       implicit val dateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
       {
        ud => new java.sql.Date(ud.getTime)
       }, {
         sd => new java.util.Date(sd.getTime)
       })

       val findById = for {
         id <- Parameters[Long]
         c <- this if c.id is id
       } yield c
     }

所以我想我的问题是我不知道如何访问指定客户的数据库条目accountBalance?

Slick的工作原理与Scala collections类似<代码>客户就像一个集合。我没有成员
accountBalance
,只有其元素具有该成员。因此,您必须使用
.map
。或者在您的情况下,可能更容易立即获取客户记录


val customerRow=Customers.filter{a=>a.id==id}.run.head

我实际上不需要提取任何客户。我只需要提取具有指定id的客户accountBalance的值。我就是这样做的:

    val accBal = Customers.filter{ a => a.id === id}.map(_.accountBalance).firstOption
由于accBal属于Option[Long]类型,我必须:

    val accBalAsLong: Long = accBal.getOrElse(0)

非常感谢。好的,这是我现在的代码:var customerRow=Customers.filter{a=>a.id==id}.map({u.accountBalance)var amount:Long=customerRow-amountToWithdraw我得到一个错误:value-不是scala.slick.lifted.Query[scala.slick.lifted.Column[Long],Long]我不能对提取的accountBalance值执行操作吗?Customers是一个查询,类似于集合。这不是一场争吵。您的变量名有误导性。作为比较,列表[客户]没有accountBalance方法。只有包含在其中的customer对象才具有该属性。我建议的行执行查询并给出结果中的第一个客户,这可能是您想要的。同时阅读流畅的文档,它会让事情变得更清楚。一般提示:使用Slick时一定要考虑Scala集合。不要想SQL。它的API与Scala集合不同。
    val accBal = Customers.filter{ a => a.id === id}.map(_.accountBalance).firstOption
    val accBalAsLong: Long = accBal.getOrElse(0)