How come I get cannot use type *sql.Row as type error when doing err == sql.ErrNoRows

huangapple go评论114阅读模式
英文:

How come I get cannot use type *sql.Row as type error when doing err == sql.ErrNoRows

问题

我试图按照这里给出的答案示例进行操作:
https://stackoverflow.com/questions/28034652/golang-how-to-check-for-empty-array-array-of-struct

来检查数据库返回是否为空。

所以我有以下代码:

err = db.QueryRow("SELECT FROM accounts WHERE steamid=?", steamid)
switch {
        case err == sql.ErrNoRows:
        case err != nil:
        default:
                //do stuff
}

但是我得到了错误信息:

cannot use db.QueryRow("SELECT FROM accounts WHERE steamid=?", steamid) (type *sql.Row) as type error in assignment:
	*sql.Row does not implement error (missing Error method)

不确定为什么在他的示例中可以工作,但在我尝试实现时却不行。谢谢。

英文:

I was trying to follow the example in the answer given here:
https://stackoverflow.com/questions/28034652/golang-how-to-check-for-empty-array-array-of-struct

on how to check if a database return is empty

So I have this:

err = db.QueryRow("SELECT FROM accounts WHERE steamid=?", steamid)
switch {
        case err == sql.ErrNoRows:
        case err != nil:
        default:
                //do stuff
}

But I get the error:

cannot use db.QueryRow("SELECT FROM accounts WHERE steamid=?", steamid) (type *sql.Row) as type error in assignment:
	*sql.Row does not implement error (missing Error method)

Not sure why it worked in his example but not when I try to implement it. Thanks.

答案1

得分: 6

你错过了示例中的Scan部分,它实际上返回了一个错误:

err := db.QueryRow("SELECT ...").Scan(&id, &secret, &shortname)
英文:

You've missed the Scan part of the example, which actually returns an error:

err := db.QueryRow("SELECT ...").Scan(&id, &secret, &shortname)

huangapple
  • 本文由 发表于 2015年8月25日 18:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/32201053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定