0
我正在使用多個結果集構建查詢,請參閱示例here。爲方便起見,下面複製重要位。使用rows.NextResultSet()在多個結果集查詢中調用rows.Err()
我的問題是,rows.Err()
應該在每個for rows.Next() {...}
循環之後調用,而不是像示例中所示的那樣在最後一次調用?爲什麼/爲什麼不?我的理解是rows.Err()
會捕獲錯誤,"... encountered during iteration",而rows.NextResultSet()
會捕獲,"... [errors] advancing to [the next result set]"。似乎我想在每次迭代期間發現錯誤,不是嗎?謝謝!
rows, err := db.Query(q, age)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var (
id int64
name string
)
if err := rows.Scan(&id, &name); err != nil {
log.Fatal(err)
}
fmt.Printf("id %d name is %s\n", id, name)
}
if !rows.NextResultSet() {
log.Fatal("expected more result sets", rows.Err())
}
var roleMap = map[int64]string{
1: "user",
2: "admin",
3: "gopher",
}
for rows.Next() {
var (
id int64
role int64
)
if err := rows.Scan(&id, &role); err != nil {
log.Fatal(err)
}
fmt.Printf("id %d has role %s\n", id, roleMap[role])
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}