string - 在 Golang 中将字符串传递给准备好的语句

标签 string postgresql go prepared-statement

我的想法是,我想更新 status 并仅在 status 不同时才返回 id。

所以,我有这样一个准备好的声明:

var theQuery = `UPDATE process SET status=$1 WHERE status!=$1 AND id=$2 RETURNING id`

然后,我这样调用它:

err = statement.QueryRow("set", 12).Scan(&id)

然后出现这样的错误。

runtime error: invalid memory address or nil pointer dereference

当我尝试时:

var theQuery = `UPDATE process SET status='$1' WHERE status!='$1' AND id=$2 RETURNING id`

它运行。然后,当我再次运行它时,我希望得到 no rows,但它仍然返回 id。看起来它仍在更新行并忽略了 status!='$1' 部分。

谢谢

最佳答案

所以,我只是找到了解决方案。准备好的语句将接收 3 个参数,而不是使用两次 $1:

var theQuery = `UPDATE process SET status=$1 WHERE status!=$2 AND id=$3 RETURNING id`

然后,我这样调用准备好的语句。

status := "set"
err = statement.QueryRow(status, status, 12).Scan(&id)

我知道这可能不是解决问题的最佳方法。但这对我有用。

关于string - 在 Golang 中将字符串传递给准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44925875/

相关文章:

python migrate (django) 不适用于 postgresql

postgresql - 连接系统,包括 OPC UA IoT Agent、Orion Context Broker。 Cygnus 和 Postgres 的历史数据

sql - 使用两个表仅列出最大日期

go - 等价于 C++ reinterpret_cast a void* to a struct in Golang

go - 在 GO 中将多部分形式的数据读取为 []byte

go - 如何判断 Go 中映射的值是否未定义?

python - 在python中查找两个字符串的所有公共(public)连续子串

java - 如何在Java中用不同的值替换字符串中多次出现的相同正则表达式模式

java - 为什么这只会打印第一行?

java - 将字符串分成两半