gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:44:42 30 4
gpt4 key购买 nike

我的想法是,我想更新 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com