Creating a generic update counter method

断了今生、忘了曾经 提交于 2019-12-22 10:02:16

问题


I'm trying to create a generic counter update method for a particular table.

My table has many columns that are simply counters, and in my application I need to increment/decrement these counters.

I was trying to create a method like this:

private def updateCounter(column: String, id: Int, incr: Int)(implicit session: Session): Unit = {
  sqlu"update table1 set $column = $column + $incr where id=$id".first
}

I would then create a method that would call this (I don't want to expose this method outside of this Dao class).

I am getting this error:

[PSQLException: ERROR: syntax error at or near "$1" Position: 20]

回答1:


Try replacing $column by #$column. $ is used for bind variables which is not suitable for identifiers like column or table names, whereas #$ is a mere string replacement like the s"" string interpolation.

Also make sure that your column variable is not vulnerable to SQL injections.



来源:https://stackoverflow.com/questions/27178052/creating-a-generic-update-counter-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!