英文:
Calling rows.Err() in a multiple result set query with rows.NextResultSet()
问题
我正在构建一个包含多个结果集的查询,按照给出的示例这里进行操作。为了方便起见,我复制了重要的部分。
我的问题是,是否应该在每个for rows.Next() {...}
循环之后调用rows.Err()
,而不是像示例中那样在最后一次调用?为什么/为什么不?我理解rows.Err()
会捕获在迭代过程中遇到的错误,而rows.NextResultSet()
会捕获"前进到下一个结果集时遇到的错误"。看起来我应该在每次迭代过程中捕获错误,对吗?谢谢!
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)
}
英文:
I'm building a query with multiple result sets, following the example given, here. Important bits copied below for convenience.
My question is, should rows.Err()
be called after each for rows.Next() {...}
loop, rather than once at the very end as shown in the example? Why / why not? My understanding is that rows.Err()
will catch errors, "... encountered during iteration", whereas rows.NextResultSet()
will catch, "... [errors] advancing to [the next result set]". Seems like I would want to catch errors during each iteration, no? Thanks!
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)
}
答案1
得分: 2
你可以从Next
、Err
或NextResultSet
函数的源代码中找到Rows
结构体的lasterr
成员,它可以通过几种方式进行设置,如果不是EOF
,Err
函数总是返回它。
这些方式包括:
Next
函数遇到错误时,会设置lasterr
并始终返回false
(因此for rows.Next() {...
不会再迭代,只需在循环后检查rows.Err()
即可)。NextResultSet
函数在遇到错误时也会设置lasterr
,并返回false
。
所以答案是肯定的,它应该这样做。因为如果NextResultSet
在Next
之后返回false
,它的错误会替换Next
的错误。
英文:
You can find from source code of Next
or Err
or NextResultSet
function that there is lasterr
member of Rows
structure which can be set by several ways and Err
always return it if it isn't EOF
.
Those ways are:
Next
gets some error, setslasterr
and always returnsfalse
(sofor rows.Next() {...
doesn't iterate more and it is enough to checkrows.Err()
only after the loop.NextResultSet
also setslasterr
whenever it gets error and also returnsfalse
.
So answer is yes, it should. Because if NextResultSet
after Next
returns false
its error replaces error from Next
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论