创建通用的更新计数器方法 [英] Creating a generic update counter method
问题描述
我正在尝试为特定表创建通用计数器更新方法.
我的表中有许多列只是计数器,在我的应用程序中,我需要增加/减少这些计数器.
我正在尝试创建这样的方法:
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
. $
用于绑定变量,它不是适用于列或表名称之类的标识符,而 #$
只是像s""
字符串插值这样的字符串替换.
还要确保您的列变量不容易受到SQL注入的攻击.</p>
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]
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.
这篇关于创建通用的更新计数器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!