我正在尝试为特定表创建通用计数器更新方法。
我的表有许多只是计数器的列,在我的应用程序中,我需要增加/减少这些计数器。
我试图创建一个这样的方法:
private def updateCounter(column: String, id: Int, incr: Int)(implicit session: Session): Unit = {
sqlu"update table1 set $column = $column + $incr where id=$id".first
}
然后我将创建一个调用它的方法(我不想在这个 Dao 类之外公开这个方法)。
我收到此错误:
[PSQLException: ERROR: syntax error at or near "$1" Position: 20]
最佳答案
尝试替换 $column
通过 #$column
. $
is used for bind variables这不适用于列名或表名等标识符,而 #$
is a mere string replacement like the s""
string interpolation .
还要确保您的列变量不易受到 SQL 注入(inject)的影响。
关于scala - 创建通用更新计数器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27178052/