Updating db row scala slick

后端 未结 1 1482
野趣味
野趣味 2021-01-08 00:52

I have following code which inserts row into table named luczekInfo and function to get data from database. My question is how to make function to update columns from table

1条回答
  •  清酒与你
    2021-01-08 01:23

    Slick 2.X

    You can update a row in two ways (as far as I know), the first one would be to create a row object of type luczekInfo#TableElementTypeand use it to update the full row:

    def updateById(id: Long, row: luczekInfo#TableElementType)(implicit s: Session): Boolean =
      luczekInfo.filter(_.id === id).update(row)
    

    Or you can update single fields using:

    def updateNameById(mId: Long, mName: String)(implicit s: Session) = {
      val q = for { l <- luczekInfo if l.id === mId } yield l.name
      q.update(mName).run
    }
    

    Where I supposed your table has a file called name.

    You can find it also on the Slick documentation in the section on updating.

    Slick 3.1.X

    there's an additional support for the insertOrUpdate (upsert) operation:

    luczekInfo.insertOrUpdate(row)
    

    0 讨论(0)
提交回复
热议问题