Golang Go-SQLite3 无法迭代类型错误。

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

Golang Go-SQLite3 cannot iterate over type error

问题

尝试使用github.com/mattn/go-sqlite3存储库中的示例时,我在尝试使用Go 1.5.1 darwin/amd64编译代码时遇到以下错误:

非布尔值 rows.Next() (类型错误) 用作条件

我正在使用的代码是:

conn, err := sqlite3.Open("./example.db")
if err != nil {
    log.Panic(err)
}
defer conn.Close()
rows, err := conn.Query("SELECT * FROM scans ORDER BY id DESC;")
if err != nil {
    log.Panic(err)
}
for rows.Next() {
    var id int
    var method string
    var uuid string
    var scan int
    rows.Scan(&id, &method, &uuid, &scan)
    log.Print(id, method, uuid, scan)
}

这里有什么我漏掉的东西吗?这是基于此处找到的示例:
https://github.com/mattn/go-sqlite3/blob/master/_example/simple/simple.go#L81-L91

英文:

When attempting to use the examples from github.com/mattn/go-sqlite3's repository, I get the following error when trying to compile the code with Go 1.5.1 darwin/amd64:

non-bool rows.Next() (type error) used as for condition

The code I'm using is:

conn, err := sqlite3.Open("./example.db")
if err != nil {
    log.Panic(err)
}
defer conn.Close()
rows, err := conn.Query("SELECT * FROM scans ORDER BY id DESC;")
if err != nil {
    log.Panic(err)
}
for rows.Next() {
    var id int
    var method string
    var uuid string
    var scan int
    rows.Scan(&id, &method, &uuid, &scan)
    log.Print(id, method, uuid, scan)
}

Is there something which I am missing here? This is based on the example found here:
https://github.com/mattn/go-sqlite3/blob/master/_example/simple/simple.go#L81-L91

答案1

得分: 4

是的,你错了。

你没有使用database/sql包,而是使用了sqlite3包!


sql.Open()返回一个sql.DBDB.Query()返回一个sql.Rows,而Rows.Next()是:

func (rs *Rows) Next() bool

但是你调用了sqlite3.Open(),它返回一个sqlite3.Conn,然后你调用了Conn.Query(),它返回一个sqlite3.Stmt,你将其命名为**rows!所以rows.Next()实际上是Stmt.Next()**,它是这样定义的:

func (s *Stmt) Next() error

混淆的来源

这是令人困惑的,因为sqlite3是一个符合内置database/sql接口的驱动程序,但它还提供了另一个接口,你通过它使用了它的特定于供应商的sqlite3接口。

使用database/sql包,你应该这样开始:

db, err := sql.Open("sqlite3", "./foo.db")
英文:

Yes, you are missing.

You are not using the database/sql package but you are using the sqlite3 package!


sql.Open() returns an sql.DB, DB.Query() returns an sql.Rows, and Rows.Next() is:

func (rs *Rows) Next() bool

But instead you call sqlite3.Open() which returns an sqlite3.Conn, then you call Conn.Query() which returns an sqlite3.Stmt which you name rows! So rows.Next() is Stmt.Next() which is:

func (s *Stmt) Next() error

Source of confusion

It is confusing because sqlite3 is a driver conforming to the built-in database/sql interface, but it also provides another interface and you used it via its vendor-specific sqlite3 interface.

Using the database/sql package you would start it like this:

db, err := sql.Open("sqlite3", "./foo.db")

huangapple
  • 本文由 发表于 2015年11月18日 05:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/33766976.html
匿名

发表评论

匿名网友

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

确定