【发布时间】:2015-05-10 18:33:09
【问题描述】:
我刚开始尝试从 sqlite 数据库中检索数据。 我使用 github.com/mattn/go-sqlite3 作为 sqlite 驱动程序。
我发送的查询没有返回任何结果,即使它应该返回。我尝试了我的程序手动生成的查询,当我手动使用查询以及通过程序发送查询时,它会返回应有的数据。
这是我的代码:
for index := range Array {
id, _ := strconv.Atoi(Array[index])
rand.Seed(time.Now().UnixNano())
RandomNr := rand.Intn(100)
fmt.Printf("index: %d - randomnr: %d \n", id, RandomNr)
rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '%d' AND '%d' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC \n",id, RandomNr)//.Scan(&idr, &name, &link, &url, &characterchance, &chance)
//this is what the finished query looks like and it returns the rows as its supposed to
//rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '203' AND '33' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC")
if errROW != nil {
fmt.Println("errROW")
log.Println(errROW)
}
defer rows.Close()
if rows.Next() == false {
fmt.Println("no rows ")
}
for rows.Next() {
fmt.Println("where are the rows")
var id int
var name string
var link string
var url string
var characterchance int
var chance int
rows.Scan(&id, &name, &link, &url, &characterchance, &chance)
fmt.Println(id,name,link,url,characterchance,chance)
}
rows.Close()
}
}
此查询可以返回多行和单行。我还尝试通过 QueryRow 将数据作为单行检索,但也没有返回任何结果。
任何帮助将不胜感激。
更新: 我添加了 如果 rows.Next() == false
作为寻找问题的尝试。删除它会产生相同的结果。此外,我没有从扫描中收到错误消息。 for rows.next() 循环甚至没有被执行。
【问题讨论】: