douzhan1031 2015-11-17 21:05
浏览 82
已采纳

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

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条回答 默认 最新

  • dongzhouzhang8696 2015-11-17 21:19
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64