dongsu1951 2014-08-20 19:28 采纳率: 0%
浏览 62
已采纳

golang sqlite无法定义查询变量

It seems golang's sqlite package doesn't like my db.Query statement, though it's exactly like the one found in the example on github.

db, err := sql.Open("sqlite3", "./database.db")
if err != nil {
    log.Fatal(err)
}
defer db.Close()

rows, err = db.Query("select id, name from job")
if err != nil {
    log.Fatal(err)
}   
defer rows.Close()

fmt.Println("Jobs:")
for rows.Next() {
    var name string
    var id int
    fmt.Printf("%v %v
", id, name)
}  

This is the error I'm getting:

./test.go:7: undefined: rows
./test.go:7: cannot assign to rows
./test.go:11: undefined: rows
./test.go:14: undefined: rows

Edit: I've tried using grave accent and single quote strings for db.Query() as well, to no avail.

  • 写回答

1条回答 默认 最新

  • doudi4137 2014-08-20 19:44
    关注

    You cannot assign values to to undeclared variables.

    rows, err = db.Query("select id, name from job")
    

    Should be :

    rows, err := db.Query("select id, name from job")
    

    Theoretically this should solve the problem, but I haven't tried.

    You should also add :

    rows.Scan(&id, &name)
    

    Before the printf function so as to actually assign the row's value to the id & name variables otherwise will print an empty string & 0.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助