英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论