Postgres sqlx准备了带有表名bindvar的语句 [英] Postgres sqlx prepared statement with table name bindvar
问题描述
我试图在使用Golang sqlx库时创建一个准备好的语句。我希望表名是一个bindVar
I am trying to create a prepared statement in using the Golang sqlx library. I want to have the table name be a bindVar
stmt, err := stmtTx.Preparex("SELECT * FROM $1 WHERE question_id=$2;")
然而,这给了我一个 / $ 1附近的语法错误/
。我可以不使用绑定var的表名吗?
However this gives me an syntax error around /$1/
. Can I not use a bind var for the table name?
推荐答案
var表格名称?
Can I not use a bind var for the table name?
只能使用参数作为数据值,而不是标识符。
因此,例如,这是合理的:
The arguments can only be used as data values, not as identifiers. Thus for example this is reasonable:
INSERT INTO mytable VALUES ($1);
但这不起作用:
but this will not work:
INSERT INTO $1 VALUES (42);
但是您可以使用 fmt.Sprintf
作为表名,如果你想要的话,但是保留数据值的$ 1,$ 2,...
But you can use fmt.Sprintf
for the table name if you want but leave the $1, $2, ... for the data values.
这篇关于Postgres sqlx准备了带有表名bindvar的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!