如何在database/sql中使用rows.NextResultSet()函数

huangapple go评论90阅读模式
英文:

How To Use rows.NextResultSet() with database/sql

问题

rows.NextResultSet()在第一个结果集之后返回false...文档上看起来很简单,但对我来说不起作用。我正在使用github.com/denisenkom/go-mssqldb

这是我的代码示例:

rows, err := db.Query(`
   Query1;
   Query2;
   Query3;
`)

for rows.Next() {
// 扫描
// 检查错误
}

if !rows.NextResultSet() {
   log.Println("期望有另一个结果集。")
// 返回一些错误
}

for rows.Next() ...
// 继续处理第二个结果集,但代码不会运行到这一点
英文:

rows.NextRestultSet() is returning false after the first set for rows... The documentation makes this sound straight forward, but it's not working for me. I'm using github.com/denisenkom/go-mssqldb.

Here is an example of my code:

rows, err := db.Query(`
   Query1;
   Query2;
   Query3;
`)

for rows.Next() {
// scan
// check error
}

if !rows.NextResultSet() {
   log.Println("Expected another result set.")
// return some error
}

for rows.Next() ...
// continue process with 2nd result set, but code will not run up until this point

答案1

得分: 2

rows.NextResultSet()返回false表示没有下一组行。你应该在一个do-while结构的循环中使用它!

for cont := true; cont; cont = rows.NextResultSet() {
  for rows.Next() {
    // 扫描
    // 检查错误
  }
}
英文:

rows.NextResultSet() would return false suggests that there is no next set of rows. You should use it in a do-while structured loop!

for cont := true; cont; cont = rows.NextResultSet() {
  for rows.Next() {
    // scan
    // check error
  }
}

答案2

得分: 0

好的,以下是翻译好的内容:

好的,我的错误在于最初使用了if rows.Next()而不是for,因为:

  1. 我只期望最多有一行数据。
  2. 如果没有行数据,我想返回一个错误,而这是最优雅的方法。

问题在于,即使只有一行数据,调用一次rows.Next()并不会将你移到行的末尾!这是显而易见的,因为在读取任何数据之前,通过对行进行迭代时会先调用Next()...

所以对于我来说,如果我确定只有一行数据,解决方法是在调用rows.NextResultSet()之前再添加一个rows.Next(),或者为了安全起见,最好使用for rows.Next()循环。

编辑:为了明确起见,我尝试使用for rows.Next()来测试是否是我做错了。但是在多个检查点处的错误消息是相同的,所以我没有意识到for循环解决了我的问题,错误是在稍后抛出的...

英文:

Okay, my mistake was that I was using if rows.Next() instead of for initially because 1. I was only expecting up to one row. 2. I wanted to return an error if there were no rows, and this was the most elegant way of doing that.

The problem is that even if there is one row, calling rows.Next() once will not take you to the end of the rows! This should be obvious since iterating through the rows starts with a call to Next() before reading any data...

So the solution for me, if I know for sure there is only one row, would be to add another rows.Next() before I call rows.NextResultSet(), or to be safe of course a for rows.Next() loop would be best.

Edit: To be clear, I was trying for rows.Next() to test if that was what I was doing wrong. But the error messages at the multiple checks were the same and so I messed up in not realizing that the for loop was solving my problem and the error was being thrown later on...

huangapple
  • 本文由 发表于 2021年12月7日 21:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70261149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定