我是 Golang 的新手,正在使用 postgres 的 PQ 包。我想做的是防止重复的电子邮件,所以我有一个查询来检查用户电子邮件是否已经在数据库中
check_duplicate_emails, err := db.Prepare("select count(*) from profiles where email=$1")
rows, err := check_duplicate_emails.Exec(email)
if rows != nil {
fmt.Fprintf(w,"Duplicate Email")
}
上面就是我的代码,我怎样才能让它像这样检查
if rows >0 { ...}
当我尝试这样做时,我得到了错误
invalid operation: rows > 0 (mismatched types sql.Result and int)
我如何解决这个问题,因为我现在一直在四处寻找解决办法。
最佳答案
这里发生的事情是你告诉 Go 你的查询不会返回任何行(参见 docs for Exec())
您可能应该使用其中之一:
关于Golfing postgres 我怎样才能从查询中返回计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39736530/