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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了