Go PostgreSQL:如何从db.Query获取行数?

时间:2015-10-07 12:49:16

标签: postgresql go

作为PostgreSQL连接器,我导入以下包:

_ "github.com/lib/pq"

我运行的查询是:

res, err := db.Query("SELECT id FROM applications WHERE email='" + email + "'")

其中电子邮件自然是一个字符串。计算res中行数的一种方法是通过以下代码段

count:=0
for res.Next() {
  count++
  //some other code
}

但应该有一些更简单(更快)的方式。似乎RowsAffected()不是要走的路。那么,你的建议是什么?

1 个答案:

答案 0 :(得分:1)

使用COUNT功能:

"SELECT count(*) FROM applications WHERE email='" + email + "'"